How to add linker flag for libraries with CMake ?

2019-02-07 18:39发布

问题:

When linking a binary I can use CMAKE_EXE_LINKER_FLAGS to add a flag (let's say -Wl,-as-needed). However, if I link a library this extra flag will not be taken into account. I would need something like CMAKE_LIB_LINKER_FLAGS but I can't find it.

How should I do ?

回答1:

You can use CMAKE_SHARED_LINKER_FLAGS like:

set (CMAKE_SHARED_LINKER_FLAGS "-Wl,--as-needed")

This question looks like related.

UPD
Thanks to @Bruce Adams who points out that since v3.13 CMake has special command for such purpose: add_link_options.



回答2:

checkout the ucm_add_linker_flags macro of ucm - it deals with appending linker flags to the appropriate cmake variables



回答3:

Looks like this problem is related to the one I had in CLION. Solved it by adding:

{set(CMAKE_CXX_STANDARD_LIBRARIES -ljpeg)} 

to CMakeLists.txt



标签: cmake