4

我正在尝试从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当我尝试使用使用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")

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

4

2 回答 2

5

我强烈建议您使用 CMake 集成的 FindPackage。CMake 将为您找到 boost 和 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}
)
于 2012-12-21T19:26:25.130 回答
2

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

这里链接器正在寻找一个库libboost_system,-lpthread.so。在任何 UNIX 系统上都不太可能存在这样的库。

你可能想要:

set(CMAKE_EXE_LINKER_FLAGS "-lboost_system -lpthread")
于 2012-12-18T06:36:45.770 回答