我的C++项目使用Boost,但当作为子模块使用时,它无法找到Boost了。

huangapple go评论80阅读模式
英文:

My c++ project uses boost but when used as a submodule it does not find boost anymore

问题

我有一个依赖于 boost 并运行良好的项目 "A"。

我创建了一个新项目 "B",在其中我将 "A" 作为子模块包含进来。然而,当我尝试编译项目 "A" 时,它无法再找到 boost 文件。我正在使用 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

其中 "boost/math/distributions.hpp" 在我的 "some_file.h" 中被使用。当项目 A 不作为子模块使用时,项目可以无错误构建并找到 boost 文件。因此,我推断错误可能在 CMakeLists.txt 文件中。

为什么在将 A 作为子模块使用时会出问题呢?

英文:

I have a project "A" that depends on boost, and works quite well.

I created a new project "B", where I included "A" as a submodule. However when I try to compile my project "A" cannot find the boost files anymore. I am using cmake, and from what I searched so far, it seems that the "CMakeLists.txt" for "B" needs to be modified but I fail to understand where...

In the CMakeLists.txt of project A I invoke boost as follows:

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

And in project B I included project A as follows:

add_subdirectory(extern/project_A)

When I include the following in my main.cpp in project B:

#include "extern/project_A/some_file.h"

some code...

I get the following error:

cannot open source file "boost/math/distributions.hpp"
'boost/math/distributions.hpp' file not found

where "boost/math/distributions.hpp" is used in my "some_file.h". When project A is not used as a submodule, the project builds without error and the boost files are found. From there I presume that my error lies in the CMakeLists.txt file.

Why is it breaking down when A is used as a submodule?

答案1

得分: 2

通常在 CMake 中,你应该使用 target_* 来为目标添加设置,而不是使用基于目录的命令。目标设置会自动传播到依赖项。

link_libraries 仅适用于在 link_libraries 调用之后创建的目标,且仅适用于当前目录或子目录中的目标。因此 link_libraries(Boost::boost) 设置不会传播到 project A 外的 project B 或其他使用它的地方。

如果你使用:
target_link_libraries(projectA Boost::boost)
无论从哪个目录调用以下命令:
target_link_libraries(projectB projectA)
projectB 将自动链接到 boost 并具有可用的包含文件。

英文:

In general in cmake you should add settings to targets with target_* rather than using the directory based commands. Target settings are automatically propagated to dependencies.

link_libraries only applies to targets created after the link_libraries call and only to targets in the current directory or subdirectory. Therefore the link_libraries(Boost::boost) setting isn't propagated outside of project A to project B or anything else that uses it.

If instead you use:

target_link_libraries(projectA Boost::boost)

Then regardless of which directory you call the following from:

target_link_libraries(projectB projectA)

projectB will automatically link to boost and have the includes available.

答案2

得分: 0

所以,您的原始问题描述实际上没有多大意义:

我的C++项目使用Boost,但当作为子模块使用时,它无法找到Boost了。

现在您已添加实际消息:

无法打开源文件 "boost/math/distributions.hpp"
'boost/math/distributions.hpp' 文件未找到

这不是 CMake 无法找到 boost。这是您的编译器在编译 您的程序 时找不到包含文件。

配置您的程序以使用 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 模块,如果您的程序依赖于库模块,它们知道如何做:

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

英文:

So, your original problem descrittion didn't really make much sense:

我的C++项目使用Boost,但当作为子模块使用时,它无法找到Boost了。

Now that you added the actual message:

cannot open source file "boost/math/distributions.hpp"
'boost/math/distributions.hpp' file not found

That's NOT Cmake not finding boost. That's your compiler not finding the include while compiling your program.

Configure your program to use boost. E.g.

INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS})
LINK_DIRECTORIES("${Boost_LIBRARY_DIRS}")

Or for the specific target:

TARGET_INCLUDE_DIRECTORIES(your_program ${Boost_INCLUDE_DIRS})
TARGET_LINK_DIRECTORIES(your_program "${Boost_LIBRARY_DIRS}")

Alternatively the Boost CMake modules know how to do it iff your program depends on a library module:

target_link_libraries(your_program Boost::system)

However there may not be a math module, nor would it usually be required: https://valelab4.ucsf.edu/svn/3rdpartypublic/boost/libs/math/doc/sf_and_dist/html/math_toolkit/main_overview/building.html

huangapple
  • 本文由 发表于 2023年3月31日 19:48:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75898193.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定