[CMake] Linking header-only lib with external lib dependency in a subfolder

Pablo Hernández pablo.hernandez.cerdan at outlook.com
Mon Mar 2 21:06:47 EST 2015


I am generating libs from header-only source files, I have a simple header with no dependencies, and a header with boost_filesystem and boost_system dependencies.
I am facing linking problems (ld)  against boost, when I link the library to the test executable. Tests are in a subdirectory, with an extra CMakeLists.txt. 
With the no-dependency header, I have no problems.
Top CMakeLists.txt:
add_library(histo-header histo-header.h)
set_target_properties(histo-header PROPERTIES LINKER_LANGUAGE CXX)
FILE(COPY histo-header.h DESTINATION include)

test/CMakeLists.txt:
add_executable(test_histo.exe test_histo.cpp)
target_link_libraries(test_histo.exe ${test_libraries})
target_link_libraries(test_histo.exe histo-header)

All good here.

But when I do the same with the library with boost dependencies:
Top CMakeLists.txt:
    find_package(Boost COMPONENTS
            system filesystem
            REQUIRED)
    include_directories(${Boost_INCLUDE_DIRS})
    add_library(histo-header-boost histo-header-boost.h )
    target_link_libraries(histo-header-boost PUBLIC ${Boost_Libraries})
    set_target_properties(histo-header-boost PROPERTIES LINKER_LANGUAGE CXX)
    FILE(COPY histo-header-boost.h DESTINATION include)

test/CMakeLists.txt:
SET( Boost_LIBRARIES ${Boost_LIBRARIES} PARENT_SCOPE )
add_executable(test_histo-boost.exe test_histo-boost.cpp )
target_link_libraries(test_histo-boost.exe ${test_libraries})
target_link_libraries(test_histo-boost.exe histo-header-boost)

I face this error:

CMakeFiles/test_histo-boost.exe.dir/test_histo-boost.cpp.o: In function `__static_initialization_and_destruction_0(int, int)':
test_histo-boost.cpp:(.text+0x1c2): undefined reference to `boost::system::generic_category()'
test_histo-boost.cpp:(.text+0x1ce): undefined reference to `boost::system::generic_category()'
test_histo-boost.cpp:(.text+0x1da): undefined reference to `boost::system::system_category()'
collect2: error: ld returned 1 exit status
m

What am I missing here?

So 
 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/cmake/attachments/20150303/4bc60bd8/attachment.html>


More information about the CMake mailing list