使用 {fmt} 作为 CMake 依赖项的问题

Issue on using {fmt} as a dependency with CMake

我正在尝试在我的项目中应用现代 CMake 实践。 我想出了 {fmt} library 依赖项的问题。

项目结构如下(简要):

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)
     ***************************

当我尝试构建它时收到错误消息:

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

可在此处找到完整的复制品: https://bitbucket.org/ngrodzitski/cmake-issue-fmt-20180410

对这个问题有什么建议吗?

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

  1. TARGET_LINK_LIBRARIES(mylib INTERFACE fmt::fmt-header-only) in mylib/CMakeLists.txt (PUBLIC before).
  2. 将以下内容添加到根目录 CMakeLists.txt:add_subdirectory(mylib)(进行更改的内容)。

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