我有一个依赖于 boost 的项目“A”,并且运行良好。
我创建了一个新项目“B”,其中我将“A”作为子模块包含在内。但是,当我尝试编译我的项目时,“A”再也找不到增强文件了。我正在使用 cmake,从目前的搜索来看,“B”的“CMakeLists.txt”似乎需要修改,但我不明白在哪里......
在项目A的CMakeLists.txt中我调用boost如下:
find_package(Boost REQUIRED OPTIONAL_COMPONENTS iostreams filesystem)
link_libraries(Boost::boost)
if (Boost_FILESYSTEM_FOUND)
message("Boost filesystem: Found")
else()
message("Boost filesystem: Not Found")
endif()
if (Boost_IOSTREAMS_FOUND)
message("Boost iostreams: Found")
else()
message("Boost iostreams: Not Found")
endif()
在项目 B 中,我将项目 A 包含如下:
add_subdirectory(extern/project_A)
当我在项目 B 的 main.cpp 中包含以下内容时:
#include "extern/project_A/some_file.h"
some code...
我收到以下错误:
cannot open source file "boost/math/distributions.hpp"
'boost/math/distributions.hpp' file not found
我的“some_file.h”中使用了“boost/math/distributions.hpp”。当项目 A 未用作子模块时,项目构建没有错误,并且找到了 boost 文件。从那里我认为我的错误在于 CMakeLists.txt 文件。
为什么A作为子模块会崩溃?
所以,你原来的问题描述并没有多大意义:
现在您添加了实际消息:
cannot open source file "boost/math/distributions.hpp"
'boost/math/distributions.hpp' file not found
那不是 Cmake 没有找到提升。那是你的编译器在编译你的程序时没有找到包含。
配置您的程序以使用 boost。例如
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS})
LINK_DIRECTORIES("${Boost_LIBRARY_DIRS}")
或针对特定目标:
TARGET_INCLUDE_DIRECTORIES(your_program ${Boost_INCLUDE_DIRS})
TARGET_LINK_DIRECTORIES(your_program "${Boost_LIBRARY_DIRS}")
或者,Boost CMake 模块知道如何去做iff 你的程序依赖于一个库模块:
target_link_libraries(your_program Boost::system)
但是可能没有数学模块,通常也不需要:https://valelab4.ucsf.edu/svn/3rdpartypublic/boost/libs/math/doc/sf_and_dist/html/math_toolkit/main_overview/building。 html