Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 279
Claudiu Avatar asked Jul 17 '14 22:07

Claudiu


1 Answers

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

like image 65
πάντα ῥεῖ Avatar answered Sep 28 '22 17:09

πάντα ῥεῖ