[CMake] Avoid re-linking dependency of external shared lib in a new project.

Pablo Hernández pablo.hernandez.cerdan at outlook.com
Tue Mar 3 20:10:47 EST 2015



        I want to generate in cmake a header-only library from header.h, that depends on libboost_system and filesystem.
I can compile the library without problems with:


find_package(Boost COMPONENTS
        system filesystem
        REQUIRED)
include_directories(SYSTEM ${Boost_INCLUDE_DIRS})
add_library(mylib SHARED header.h)
target_link_libraries(mylib PUBLIC ${Boost_LIBRARIES})
set_target_properties(mylib PROPERTIES LINKER_LANGUAGE CXX)

But when I link to mylib elsewhere, it fails finding the boost library with a ld error.


The failure makes sense, but I don't know how to solve it in CMake.
How do I "store" the boost dependency on mylib? So I don't have to worry
 about finding the boost library in other external project?



I am using cmake 3.2


When I use it in other project, the linker is not able to find the boost library:


target_link_libraries(newproject.exe ${external_mylib})


undefined reference to symbol '_ZN5boost6system15system_categoryEv'
/PATH/TO/libboost_system-mt-d.so.1.57.0: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status

It is solved if I explicitly link again to the Boost_LIBRARIES.

target_link_libraries(newproject.exe ${external_mylib} ${Boost_LIBRARIES))




This doesn't avoid finding again the boost_libraries, maybe the 
solution is to put the boost_libraries in the environment variable 
LD_LIBRARY_PATH?

Can I avoid relinking to boost somehow?



     		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/cmake/attachments/20150304/a1e492a7/attachment.html>


More information about the CMake mailing list