1

I'm using cmake for project with boost, I've set boost-signals as required and cmake states it finds it, but when I compile the project I get a linking error.

I can resolve it with a linker directive set(CMAKE_EXE_LINKER_FLAGS -lboost_signals) but this seems contrary to what it should be doing. Can someone suggest a better way? Thanks

in CMakeLists.txt

   IF(UNIX)
        find_package(Boost COMPONENTS system filesystem random regex signals thread program_options date_time REQUIRED)

            INCLUDE_DIRECTORIES( ${Boost_INCLUDE_DIRS} ) 

    ENDIF()

Running cmake:

   $ cmake ../
    -- Boost version: 1.49.0
    -- Found the following Boost libraries:
    --   system
    --   filesystem
    --   random
    --   regex
    --   signals
    --   thread
    --   program_options
    --   date_time
    -- Configuring done
    -- Generating done
    -- Build files have been written to: /home/me/myproject/build

Linker error:

/usr/bin/ld: /usr/local/lib/libwt.so: undefined reference to symbol '_ZN5boost7signals9trackableD2Ev'
/usr/bin/ld: note: '_ZN5boost7signals9trackableD2Ev' is defined in DSO /usr/lib/libboost_signals.so.1.49.0 so try adding it to the linker command line
/usr/lib/libboost_signals.so.1.49.0: could not read symbols: Invalid operation
collect2: error: ld returned 1 exit status
shellter
  • 36,525
  • 7
  • 83
  • 90
Jim
  • 809
  • 1
  • 7
  • 18
  • Can you show us whole linker command line? I'm wondering if you have -lboost_signals added. – Bogdan Mar 21 '14 at 19:51

1 Answers1

0

Boost Signals is not a header-only library. In addition to giving the include directory, you need to tell CMake to link against it too.

add_executable(MyProgram [...] )
target_link_libraries(MyProgram ${Boost_LIBRARIES})

In case you plan to build on Windows as well, you might also want to disable auto-linking:

add_definitions(-DBOOST_ALL_NO_LIB)

Note that Boost Signals has been deprecated and replaced by Signals2, which is in fact a header-only library. If it is up to you to make the choice, consider using Signals2 instead.

Community
  • 1
  • 1
ComicSansMS
  • 51,484
  • 14
  • 155
  • 166
  • good catch.. I had target_link_libraries but I only listed some of the boost libraries like ${Boost_FILESYSTEM_LIBRARY}.. thanks. – Jim Mar 22 '14 at 11:48