4

I'm trying to apply modern CMake practices in my project. And I've came up with an issue with the {fmt} library dependency.

The structure of a project is the following (in a brief):

dev/
|
+--- fmt/   *unpacked archive of 4.1.0 version*
|
+--- mylib/
|    |
|    +--- mylib.hpp
|    |
|    +--- CMakeLists.txt
|         ***************************
|         * ...
|         * add_library(mylib INTERFACE)
|         * TARGET_LINK_LIBRARIES(mylib PUBLIC fmt-header-only)
|         * set(MYLIB_HEADERS_ALL mylib.hpp )
|         * ...
|         ***************************
|
+--- sample/
|    |
|    +--- main.cpp
|    |
|    +--- CMakeLists.txt
|         ***************************
|         * set(SAMPLE sample.hello_world)
|         * add_executable(${SAMPLE} main.cpp)
|         * TARGET_LINK_LIBRARIES(${SAMPLE} PRIVATE mylib)
|         * install(TARGETS ${SAMPLE} DESTINATION bin)
|         ***************************
|
+--- CMakeLists.txt
     ***************************
     * include_directories(${CMAKE_CURRENT_SOURCE_DIR})
     * add_subdirectory(fmt EXCLUDE_FROM_ALL)
     * add_subdirectory(sample/hello_world)
     ***************************

When I try to build it I receive an error:

PATH/mylib/mylib.hpp:6:10: fatal error: fmt/format.hpp: No such file or directory
 #include <fmt/format.hpp>
          ^~~~~~~~~~~~~~~~
compilation terminated.

Full reproduction can be found here: https://bitbucket.org/ngrodzitski/cmake-issue-fmt-20180410

Any suggestions on the issue?

4

1 回答 1

2

在松弛的 Mathieu Ropert 的帮助下,我通过以下步骤解决了这个问题:

  1. INTERFACEmylib/CMakeLists.txt(PUBLIC之前)中的TARGET_LINK_LIBRARIES(mylib fmt::fmt-header-only)。
  2. 将以下内容添加到根 CMakeLists.txt 中:(add_subdirectory(mylib)这就是改变的原因)。

我将最终版本推送到回购: https ://bitbucket.org/ngrodzitski/cmake-issue-fmt-20180410 。

于 2018-04-10T13:17:36.060 回答