使用cmake链接到boost时未定义的引用错误

时间:2014-01-01 15:42:27

标签: boost linker cmake undefined-reference

我正在尝试使用cmake编译我的项目,但我无法通过链接器错误。我已经通过apt-get install和手动在我的主文件夹中安装了boost,但是链接器没有看到它们中的任何一个。

这是我的cmake列表文件:

project(SignalAnalyzerRQ)
cmake_minimum_required(VERSION 2.8)


set(Boost_INCLUDE_DIR /home/natalia/software/boost_1_55_0)
set(Boost_LIBRARY_DIR /home/natalia/software/boost_1_55_0/stage/lib)
find_package(Boost COMPONENTS system thread timer chrono REQUIRED)
include_directories(${Boost_INCLUDE_DIR})
link_directories(${Boost_LIBRARY_DIR})


set(CMAKE_CXX_FLAGS "-lboost_system -lboost_timer -lboost_chrono -lrt -lboost_thread -lboost_thread -lgsl -lgslcblas")
add_definitions(${CMAKE_CXX_FLAGS})
aux_source_directory(. SRC_LIST)
add_executable(${PROJECT_NAME} ${SRC_LIST})

这是它给我的错误的一部分:

CMakeFiles/SignalAnalyzerRQ.dir/Tester.cpp.o: In function `boost::thread_exception::thread_exception(int, char const*)':
Tester.cpp:(.text._ZN5boost16thread_exceptionC2EiPKc[_ZN5boost16thread_exceptionC5EiPKc]+0x14): undefined reference to `boost::system::system_category()'
CMakeFiles/SignalAnalyzerRQ.dir/Tester.cpp.o: In function `boost::detail::thread_data_base::thread_data_base()':
Tester.cpp:(.text._ZN5boost6detail16thread_data_baseC2Ev[_ZN5boost6detail16thread_data_baseC5Ev]+0x24): undefined reference to `vtable for boost::detail::thread_data_base'
CMakeFiles/SignalAnalyzerRQ.dir/Tester.cpp.o: In function `boost::thread::start_thread()':
Tester.cpp:(.text._ZN5boost6thread12start_threadEv[_ZN5boost6thread12start_threadEv]+0x15): undefined reference to `boost::thread::start_thread_noexcept()'
CMakeFiles/SignalAnalyzerRQ.dir/Tester.cpp.o: In function `boost::thread::~thread()':
Tester.cpp:(.text._ZN5boost6threadD2Ev[_ZN5boost6threadD5Ev]+0x15): undefined reference to `boost::thread::detach()'
CMakeFiles/SignalAnalyzerRQ.dir/Tester.cpp.o: In function `boost::detail::thread_data<boost::_bi::bind_t<void, boost::_mfi::mf2<void, Tester, int, int>, boost::_bi::list3<boost::_bi::value<Tester*>, boost::_bi::value<int>, boost::_bi::value<int> > > >::~thread_data()':
Tester.cpp:(.text._ZN5boost6detail11thread_dataINS_3_bi6bind_tIvNS_4_mfi3mf2Iv6TesteriiEENS2_5list3INS2_5valueIPS6_EENS9_IiEESC_EEEEED2Ev[_ZN5boost6detail11thread_dataINS_3_bi6bind_tIvNS_4_mfi3mf2Iv6TesteriiEENS2_5list3INS2_5valueIPS6_EENS9_IiEESC_EEEEED5Ev]+0x1f): undefined reference to `boost::detail::thread_data_base::~thread_data_base()'
CMakeFiles/SignalAnalyzerRQ.dir/Tester.cpp.o:(.rodata._ZTIN5boost6detail11thread_dataINS_3_bi6bind_tIvNS_4_mfi3mf2Iv6TesteriiEENS2_5list3INS2_5valueIPS6_EENS9_IiEESC_EEEEEE[_ZTIN5boost6detail11thread_dataINS_3_bi6bind_tIvNS_4_mfi3mf2Iv6TesteriiEENS2_5list3INS2_5valueIPS6_EENS9_IiEESC_EEEEEE]+0x10): undefined reference to `typeinfo for boost::detail::thread_data_base'
CMakeFiles/SignalAnalyzerRQ.dir/Integrator.cpp.o: In function `Integrator::integral(std::map<float, float, std::less<float>, std::allocator<std::pair<float const, float> > >*)':
Integrator.cpp:(.text+0x5b): undefined reference to `gsl_set_error_handler_off'
Integrator.cpp:(.text+0x69): undefined reference to `gsl_integration_workspace_alloc'
Integrator.cpp:(.text+0x14a): undefined reference to `gsl_integration_qag'
Integrator.cpp:(.text+0x1b7): undefined reference to `gsl_set_error_handler'
(...)
LeastSquaresComparator.cpp:(.text+0x5c4): undefined reference to `boost::system::generic_category()'
LeastSquaresComparator.cpp:(.text+0x5d0): undefined reference to `boost::system::generic_category()'
LeastSquaresComparator.cpp:(.text+0x5dc): undefined reference to `boost::system::system_category()'

我没有在cmake列表中包含gsl的路径,所以我理解为什么有未定义的引用,但我认为boost应该有效,但事实并非如此。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:3)

1

set(Boost_INCLUDE_DIR /home/natalia/software/boost_1_55_0)
set(Boost_LIBRARY_DIR /home/natalia/software/boost_1_55_0/stage/lib)

这看起来很奇怪...... Install提升库并将BOOST_ROOT变量设置为安装 前缀:

set(BOOST_ROOT "/path/to/install/directory")
find_package(Boost REQUIRED system thread timer chrono)

请注意,如果COMPONENTS存在,则无需REQUIRED

2

link_directories(${Boost_LIBRARY_DIR})

找到的库保存到Boost_LIBRARIES变量。使用target_link_libraries链接它:

target_link_libraries(Foo ${Boost_LIBRARIES})

3

set(CMAKE_CXX_FLAGS "-lboost_system ...")

请勿使用CMAKE_CXX_FLAGS链接库,为其设计target_link_libraries命令(见上文)。

4

add_definitions(${CMAKE_CXX_FLAGS})

此命令不是为修改编译器标志而设计的,它旨在添加定义。

答案 1 :(得分:0)

对于链接,您不要使用CXX_FLAGS,而是使用类似的内容 SET_TARGET_PROPERTIES(foo PROPERTIES LINK_FLAGS -Wl,-specialFlag)其中foo是目标,您必须替换库中的-Wl..内容。