asgoodas
asgoodas

Reputation: 163

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. I am using CMAKE as it has to be cross platform.

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. This all compiles and creates the library. The cmake code is shown below.

 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. Is there a way I can do this with CMAKE?

Upvotes: 2

Views: 1456

Answers (2)

asgoodas
asgoodas

Reputation: 163

It looks like it is possible with visual studio but not generally:

Linking static libraries to other static libraries

Upvotes: 1

jhauris
jhauris

Reputation: 1143

There is an option in the Boost find module called Boost_USE_STATIC_LIBS. Set it to "ON". My canned Boost CMake looks like:

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

Upvotes: 0

Related Questions