简体   繁体   English

链接到通过cmake使用boost的库时,删除boost依赖性

[英]Remove boost dependency when linking to a library that uses boost with cmake

I am trying to create a library which uses boost filesystem within but I don't want to have to add that dependency to the executable that uses that library. 我正在尝试创建一个在其中使用boost文件系统的库,但我不想将该依赖项添加到使用该库的可执行文件中。 I am using CMAKE as it has to be cross platform. 我正在使用CMAKE,因为它必须跨平台。

In the cmake file I have added a preprocessor definition 'BOOST_ALL_NO_LIB' to not auto link and set cmake to use static libraries. 在cmake文件中,我添加了预处理器定义'BOOST_ALL_NO_LIB'来不自动链接,并将cmake设置为使用静态库。 This all compiles and creates the library. 所有这些都会编译并创建库。 The cmake code is shown below. 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})

But when I link in the library I get errors like the one below. 但是,当我在库中链接时,会出现以下错误。

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

If I add the boost filesystem and boost system libraries to the the additional dependencies in the Librarian for my Library, the errors in the executable go away. 如果我将boost文件系统和boost系统库添加到库的库管理器的其他依赖项中,则可执行文件中的错误就会消失。 Is there a way I can do this with CMAKE? 我可以用CMAKE做到这一点吗?

It looks like it is possible with visual studio but not generally: 看起来像使用Visual Studio可以实现,但通常不是这样:

Linking static libraries to other static libraries 将静态库链接到其他静态库

There is an option in the Boost find module called Boost_USE_STATIC_LIBS . Boost查找模块中有一个名为Boost_USE_STATIC_LIBS的选项。 Set it to "ON". 将其设置为“ ON”。 My canned Boost CMake looks like: 我的Boost CMake罐头看起来像:

set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
find_package(Boost ...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM