如何使用CMake在链接命令行的末尾添加标志? [英] How can I add a flag at the end of the linking command line using CMake?

查看:308
本文介绍了如何使用CMake在链接命令行的末尾添加标志?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,即CMake 无法检测到pthread .作为一种解决方法,我尝试过:

I've got an issue where CMake can't detect pthread. As a work-around I tried:

set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lpthread")

但是,这会在错误的位置插入-lpthread:

However, this inserts -lpthread in the wrong place:

/usr/bin/c++    -std=c++11 -D_GNU_SOURCE  -Wall [manyflags ...]    -lpthread \
    CMakeFiles/connectivity_tool.dir/connectivity_tool/conn_tool.cpp.o       \
    -o connectivity_tool -rdynamic -lboost_system [many libraries...]

结果是:

/usr/bin/ld: /tmp/ccNvRifh.ltrans3.ltrans.o: undefined reference to symbol 'pthread_mutexattr_settype@@GLIBC_2.2.5'
/lib/x86_64-linux-gnu/libpthread.so.0: error adding symbols: DSO missing from command line

当然,-lpthread应该在第三行的末尾,而不是第一行的末尾.

Of course, the -lpthread should be at the end of the 3rd line, not the end of the 1st.

我如何才能使CMake在此行的末尾添加-lpthread,或者甚至以某种不可靠的方式以某种方式修改生成的Makefile来使其正常工作?

How can I go about either getting CMake to add -lpthread at the end of this line, or perhaps even modifying the generated Makefiles somehow in some hacky way to get this to work?

(如果答案涉及实际检测到pthread,请回答链接的问题.)

(If the answer involves actually detecting pthread properly then answer the linked question.)

推荐答案

我该如何使CMake在此行的末尾添加-lpthread,或者甚至以某种不可靠的方式以某种方式修改生成的Makefile,以使其正常工作?"

"How can I go about either getting CMake to add -lpthread at the end of this line, or perhaps even modifying the generated Makefiles somehow in some hacky way to get this to work?"

首先请确保您的

set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lpthread")

是CMake在行中看到的最后一个.
任何其他库/模块引用(例如 FIND_BOOST )可能会弄乱您要直接提供的标志的顺序.

is the last seen in line by CMake.
Any further library/module references (like e.g. FIND_BOOST) may screw up the order of the flags you want to provide directly.

我会使用

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")

set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pthread")

我认为使用此选项,链接器会自动检测相应的pthread库,该库出现在链接器对象链的末尾.

I think with this option, the linker automatically detects the appropriate pthread library, linked appearing at the end of the linker objects chain.

这篇关于如何使用CMake在链接命令行的末尾添加标志?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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