【发布时间】:2018-04-10 09:47:16
【问题描述】:
我正在尝试在我的项目中应用现代 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
对这个问题有什么建议吗?
【问题讨论】:
-
代替
make --build运行make VERBOSE=1并检查对应的包含目录是否传递给编译器。 -
@Tsyvarev ,没有路径 ${project_root}/dev/fmt 可以解决问题。显然,我可以将
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/fmt)添加到根 CMakeLists.txt,但它来自 Mathieu Ropert 的介绍:video(幻灯片第 44 页)include_directories()被认为是有害的。在同一张幻灯片上,我说:不要将 TARGET_INCLUDE_DIRECTORIES() 与模块外的路径一起使用。 -
slides 的链接在单独的评论中,因为它太长了。
-
很奇怪,根据code,目标
fmt-header-only已经对应了附加的include目录。