gpt4 book ai didi

c++ - 如何使用 CMake 在链接命令行的末尾添加标志?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:44:08 25 4
gpt4 key购买 nike

我有一个问题,CMake can't detect pthread .作为解决方法,我尝试了:

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

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

/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应该在第3行的末尾,而不是在第1行的末尾。

我怎样才能让 CMake 在此行的末尾添加 -lpthread,或者甚至以某种 hacky 的方式修改生成的 Makefile 以使其工作?

(如果答案涉及实际正确检测 pthread,则回答链接的问题。)

最佳答案

"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)可能会打乱您想要直接提供的标志的顺序。

我会用

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

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

我认为使用这个选项,链接器会自动检测适当的 pthread 库,链接出现在链接器对象链的末尾。

关于c++ - 如何使用 CMake 在链接命令行的末尾添加标志?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24814698/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com