How can I add a flag at the end of the linking command line using CMake?

邮差的信 提交于 2020-01-02 03:47:08

问题


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

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...]

This results in:

/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

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

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?

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


回答1:


"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?"

1st be sure that your

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

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.

I would use

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

and

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

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



来源:https://stackoverflow.com/questions/24814698/how-can-i-add-a-flag-at-the-end-of-the-linking-command-line-using-cmake

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!