Boost 1.59 链接器错误

Boost 1.59 linker error

我在 Ubuntu 11.04 的较旧安装上本地安装了 Boost 库(特别是版本 1.59),我已将其设置为安装在我的主目录中的以下位置:

${HOME}/boost_1_59

我正在使用以下 CMake 文件来编译和构建我正在处理的程序:

cmake_minimum_required( VERSION 2.8 )

project( myproject C CXX )

set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin )
set( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/lib )

set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x -pthread" )

set( BOOST_ROOT "/home/user/boost_1_59" )

set( BOOST_MIN_VERSION          "1.59" )
set( Boost_USE_MULTITHREADED    ON )
set( Boost_USE_STATIC_LIBS      ON )

find_package( Boost ${BOOST_MIN_VERSION} COMPONENTS filesystem thread system REQUIRED )

if( Boost_FOUND )
    set( BOOST_LIBRARYDIR "/home/user/boost_1_59/lib" )
    set( BOOST_INCLUDEDIR "/home/user/boost_1_59/include" )

    INCLUDE_DIRECTORIES( ${Boost_INCLUDE_DIR} )

    add_executable( TorcPause main.cpp )
    target_link_libraries( TorcPause ${Boost_LIBRARIES} )
endif()

当我尝试编译项目时出现以下链接器错误(我总是清除 "build" 目录以确保 CMake 未从缓存中提取,因为这是尝试使用 boost 时的已知问题使用 CMake):

CMakeFiles/TorcPause.dir/TorcPause.cpp.o: In function `boost::asio::error::get_system_category()':
TorcPause.cpp:(.text._ZN5boost4asio5error19get_system_categoryEv[boost::asio::error::get_system_category()]+0x5): undefined reference to `boost::system::system_category()'
CMakeFiles/TorcPause.dir/TorcPause.cpp.o: In function `boost::thread_exception::thread_exception(int, char const*)':
TorcPause.cpp:(.text._ZN5boost16thread_exceptionC2EiPKc[_ZN5boost16thread_exceptionC5EiPKc]+0x14): undefined reference to `boost::system::system_category()'
CMakeFiles/TorcPause.dir/TorcPause.cpp.o: In function `boost::condition_error::condition_error(int, char const*)':
TorcPause.cpp:(.text._ZN5boost15condition_errorC2EiPKc[_ZN5boost15condition_errorC5EiPKc]+0x14): undefined reference to `boost::system::system_category()'
CMakeFiles/TorcPause.dir/TorcPause.cpp.o: In function `boost::thread::start_thread()':
TorcPause.cpp:(.text._ZN5boost6thread12start_threadEv[boost::thread::start_thread()]+0x15): undefined reference to `boost::thread::start_thread_noexcept()'
CMakeFiles/TorcPause.dir/TorcPause.cpp.o: In function `boost::thread::join()':
TorcPause.cpp:(.text._ZN5boost6thread4joinEv[boost::thread::join()]+0x6d): undefined reference to `boost::thread::join_noexcept()'
libTorcIOManager.a(IOManager.cxx.o): In function `__static_initialization_and_destruction_0(int, int)':
IOManager.cxx:(.text+0x2fd): undefined reference to `boost::system::generic_category()'
IOManager.cxx:(.text+0x309): undefined reference to `boost::system::generic_category()'
IOManager.cxx:(.text+0x315): undefined reference to `boost::system::system_category()'
libTorcIOManager.a(IOManager.cxx.o): In function `boost::system::error_code::error_code()':
IOManager.cxx:(.text._ZN5boost6system10error_codeC2Ev[_ZN5boost6system10error_codeC5Ev]+0x17): undefined reference to `boost::system::system_category()'
libTorcConnection.a(Connection.cxx.o): In function `__static_initialization_and_destruction_0(int, int)':
Connection.cxx:(.text+0x17c0): undefined reference to `boost::system::generic_category()'
Connection.cxx:(.text+0x17cc): undefined reference to `boost::system::generic_category()'
Connection.cxx:(.text+0x17d8): undefined reference to `boost::system::system_category()'
collect2: ld returned 1 exit status
make[2]: *** [../bin/TorcPause] Error 1
make[1]: *** [CMakeFiles/TorcPause.dir/all] Error 2
make: *** [all] Error 2

正如您从我的 CMake 文件中看到的那样,我包含了 boost 系统库,因此我不确定我做错了什么或者我需要更改什么才能使其正常工作。

更新 #1: 这里要求的是 运行 make VERBOSE=1

的输出

CMake & make VERBOSE=1 command line output

有两个问题:

  1. Your linker invocation 表明链接器不搜索自定义库目录中的库。因此你需要添加 LINK_DIRECTORIES(${BOOST_LIBRARYDIR})

  2. 此外,由于 Boost_USE_MULTITHREADED 已启用,boost 库 (libboost_...-mt.a) 的多线程版本需要存在于您的自定义库目录中。正如您所说,那些不存在,因此您需要在启用多线程支持的情况下重新编译 boost。