cmake和boost信号的问题

problems with cmake and boost-signals

本文关键字:问题 信号 boost cmake      更新时间:2023-10-16

我使用cmake的项目与boost,我已经设置了所需的boost信号和cmake状态找到它,但当我编译项目我得到一个链接错误。

我可以解决它与链接器指令集(CMAKE_EXE_LINKER_FLAGS -lboostrongignals),但是这似乎与它应该做的相反。有人能建议一个更好的方法吗?由于

在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()

运行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

链接器错误:

/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

Boost Signals不是一个头文件库。除了给出包含目录,你还需要告诉CMake链接到它。

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

如果你也打算在Windows上构建,你可能还想禁用自动链接:

add_definitions(-DBOOST_ALL_NO_LIB)

注意Boost Signals已经被弃用,并被Signals2取代,Signals2实际上是一个头文件库。如果由您来做选择,可以考虑使用Signals2。