提问人:Natalia Zoń 提问时间:1/1/2014 更新时间:1/2/2014 访问量:5932
使用 cmake 链接到 boost 时出现未定义的引用错误
Undefined reference errors when linking to boost using cmake
问:
我正在尝试使用 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 应该有效,但事实并非如此。任何帮助将不胜感激。
答:
0赞
usr1234567
1/2/2014
#1
对于链接,您不使用,但像 where 是目标这样的东西,您必须用您的库替换这些东西。CXX_FLAGS
SET_TARGET_PROPERTIES(foo PROPERTIES LINK_FLAGS -Wl,-specialFlag)
foo
-Wl..
3赞
user2288008
1/2/2014
#2
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)
这看起来很奇怪......安装 boost 库并将变量设置为 installation BOOST_ROOT 前缀:
set(BOOST_ROOT "/path/to/install/directory")
find_package(Boost REQUIRED system thread timer chrono)
请注意,不存在 if 的必要。COMPONENTS
REQUIRED
2
link_directories(${Boost_LIBRARY_DIR})
找到保存到变量的库。使用target_link_libraries链接它:Boost_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})
此命令不是用来修改编译器标志的,而是用来添加定义的。
评论