c++ - 链接到通过 cmake 使用 boost 的库时删除 boost 依赖项

标签 c++ visual-studio boost cmake static-libraries

我正在尝试创建一个在其中使用 boost 文件系统的库,但我不想将该依赖项添加到使用该库的可执行文件中。我正在使用 CMAKE,因为它必须是跨平台的。

在 cmake 文件中,我添加了预处理器定义“BOOST_ALL_NO_LIB”以不自动链接并将 cmake 设置为使用静态库。这一切都编译并创建了库。 cmake代码如下所示。

 set(Boost_USE_STATIC_LIBS ON)
 add_definitions(-DBOOST_ALL_NO_LIB)
 find_package(Boost REQUIRED COMPONENTS system filesystem)
 include_directories(${Boost_INCLUDE_DIRS})
 link_directories(${Boost_LIBRARY_DIR})

target_link_libraries(Utility
    ${Boost_LIBRARIES})

但是当我在库中链接时,出现如下错误。

Error   1   error LNK2001: unresolved external symbol "class boost::system::error_category const & __cdecl boost::system::system_category(void)" ...

如果我将 boost 文件系统和 boost 系统库添加到我的库的 Librarian 中的附加依赖项中,可执行文件中的错误就会消失。有什么方法可以用 CMAKE 做到这一点吗?

最佳答案

看起来用 visual studio 是可行的,但一般情况下不行:

Linking static libraries to other static libraries

关于c++ - 链接到通过 cmake 使用 boost 的库时删除 boost 依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20681194/

相关文章:

c++ - const char& 作为函数的参数

c++ - 使用 boost 正则表达式提取所有破折号分隔的数字

c++ - 模板特化 - 提供可选模板

c# - 自定义 Intellisense Presenter 问题

c++ - 您的计算机中缺少 api-ms-win-core-winrt-string-l1-1-0.dll

visual-studio - Visual Studio "Go to implementation "

c++ - 编写通用包装器 : Conditionally Map different Types from Template Arguments onto a Single Class-Internal Type

c++ - bazel 项目在哪里存储它的 ".so"文件?

c++ - 命名空间内的类的友元函数的定义。

c++ - 使用while循环确定数字的平方根c++