48

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Barth
  • 15,135
  • 20
  • 70
  • 105
  • 3
    take a look at CMakeCache.txt file. There are various CMAKE_*_LINKER_FLAGS variables (I do not have cmake currently so i cannot check). Choose the one you want – Michał Walenciak Jul 02 '14 at 14:06

7 Answers7

54

Note: modern CMake has a better solution than mentioned below (see updates for details).

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.

UPD 2
Thanks to @Alex Reinking who points out that modern CMake doesn't recommend using global settings. It is suggested to give the preference to the property settings before the global ones, so instead of add_link_options that has a global scope, the target_link_options should be used. See Alex's answer for details.

Gluttton
  • 5,739
  • 3
  • 31
  • 58
16

This is how you add linker flags to a target in modern CMake (3.13+):

# my_tgt can be an executable, library, or module.
target_link_options(my_tgt PRIVATE "LINKER:-as-needed")

Note that CMake always passes flags to the configured compiler. Thus, to forward your intended link flags to the linker, you must use the LINKER: prefix. CMake will take care of expanding it to -Wl,-as-needed on GCC, and to -Xlinker -as-needed on Clang.

See the documentation here: https://cmake.org/cmake/help/latest/command/target_link_options.html

Alex Reinking
  • 16,724
  • 5
  • 52
  • 86
4

It looks like this problem is related to the one I had in CLION. I solved it by adding

{set(CMAKE_CXX_STANDARD_LIBRARIES -ljpeg)}

to CMakeLists.txt.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
JVamcas
  • 122
  • 3
  • 12
  • 4
    **DO NOT USE THIS VARIABLE THAT WAY.** [The documentation](https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_STANDARD_LIBRARIES.html?highlight=standard_libraries#variable:CMAKE_%3CLANG%3E_STANDARD_LIBRARIES) clearly states _"This variable should not be set by project code. It is meant to be set by CMake’s platform information modules for the current toolchain, or by a toolchain file when used with CMAKE_TOOLCHAIN_FILE."_ – Alex Reinking Jan 28 '21 at 10:50
3

In CMake 3.10.2, the suggested answer of set (CMAKE_SHARED_LINKER_FLAGS "-Wl,--as-needed") did not work for me. The workaround I employed was to use set_target_properties instead.

My CMakeLists.txt file had a line of this sort: add_library(libraryname MODULE a.cc b.cc c.cc)

After that line, I added this: set_target_properties(libraryname PROPERTIES LINK_FLAGS "-Wl,-znodelete")

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mike Spear
  • 832
  • 8
  • 13
2

Check out the ucm_add_linker_flags macro of ucm - it deals with appending linker flags to the appropriate CMake variables.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
onqtam
  • 4,356
  • 2
  • 28
  • 50
0

Note that you need to add your linker option without the "-Wl,", namely:

Good:

-DCMAKE_SHARED_LINKER_FLAGS="-fstack-protector"

Wrong:

-DCMAKE_SHARED_LINKER_FLAGS="-Wl,-fstack-protector"
Michał Leon
  • 2,108
  • 1
  • 15
  • 15
-1

target_link_libraries(target-name PRIVATE -lexpat)

i used this for linking the XML dependencies in my project. replace -lexpat with -l(library name)

MSharq
  • 65
  • 1
  • 3