cmake为boost设置链接器标志

时间:2012-12-17 17:36:51

标签: linker cmake

我正在尝试从http://www.boost.org/doc/libs/1_36_0/doc/html/boost_asio/tutorial/tuttimer1.html编译一个boost教程示例。

我的CMakeLists.txt如下所示:

project(boost)
add_executable(timer1 timer1.cpp)
set_target_properties(timer1 PROPERTIES LINK_FLAGS -lboost_system,-lpthread)

尝试用cmake构建整个东西,我得到:

/var/www/C++/boost/build$ make
-- Configuring done
-- Generating done
-- Build files have been written to: /var/www/C++/boost/build
Scanning dependencies of target timer1
[100%] Building CXX object CMakeFiles/timer1.dir/timer1.cpp.o                                                                                                    
Linking CXX executable timer1                                                                                                                                    
/usr/bin/ld: cannot find -lboost_system,-lpthread                                                                                                                
collect2: ld returned 1 exit status
make[2]: *** [timer1] Błąd 1
make[1]: *** [CMakeFiles/timer1.dir/all] Błąd 2
make: *** [all] Błąd 2

但是当我跑步时:

g++ timer1.cpp -lboost_system -lpthread -o timer1

手动,一切正常。有人可以指点我做错了吗?

PS 当我尝试使用Turning on linker flags with CMake中描述的解决方案时,我向cmake添加以下行:

set(CMAKE_SHARED_LINKER_FLAGS "-lboost_system,-lpthread")
set(CMAKE_MODULE_LINKER_FLAGS "-lboost_system,-lpthread")
set(CMAKE_EXE_LINKER_FLAGS "-lboost_system,-lpthread")

我得到与上面相同的错误。

2 个答案:

答案 0 :(得分:3)

我强烈建议您使用CMake集成的FindPackage。 CMake会为你找到提升和pthreads。

您的CMakeLists.txt应如下所示:

find_package( Boost COMPONENTS thread system filesystem REQUIRED ) #whatever libs you need
include_directories( ${Boost_INCLUDE_DIRS} )
find_package( Threads )

在子文件夹src中:

set( LIBS_TO_LINK
    ${Boost_LIBRARIES}
    ${CMAKE_THREAD_LIBS_INIT}
)

target_link_libraries( myApp
    ${LIBS_TO_LINK}
)

答案 1 :(得分:2)

  

/ usr / bin / ld:找不到-lboost_system,-lpthread

此处链接器正在寻找库libboost_system,-lpthread.so在任何UNIX系统上都存在

你可能想要:

set(CMAKE_EXE_LINKER_FLAGS "-lboost_system -lpthread")