cmake设置链接器标志以增强 [英] cmake set linker flags for boost

查看:216
本文介绍了cmake设置链接器标志以增强的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从我的CMakeLists.txt如下所示:

My CMakeLists.txt looks like the following:

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

尝试使用cmake构建整个内容,我得到:

trying to build the whole thing with cmake, I get:

/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

手动,一切正常.有人可以指出我该怎么做吗?

manually, everything works fine. Can someone please point me on what do I do wrong?

PS 当我尝试使用使用CMake启用链接器标记中所述的解决方案时,我添加以下命令:

PS When I try to use solution described in Turning on linker flags with CMake, I add to cmake the following lines:

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

我得到与上述相同的错误.

and I get the same error as above.

推荐答案

我强烈建议您使用CMake集成的FindPackage. CMake将为您找到boost和pthreads.

I strongly suggest that you use the CMake integrated FindPackage. CMake will find boost and pthreads for you.

您的CMakeLists.txt应该如下所示:

Your CMakeLists.txt should look like this:

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}
)

这篇关于cmake设置链接器标志以增强的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆