41

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
  • 28,342
  • 21
  • 95
  • 123
Barth
  • 13,295
  • 17
  • 63
  • 99
  • 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

5 Answers5

49

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,137
  • 2
  • 27
  • 53
  • 4
    Note that CMAKE_SHARED_LINKER_FLAGS is for dynamic libraries. For static libraries use CMAKE_STATIC_LINKER_FLAGS instead. – Shital Shah Jul 20 '17 at 02:47
  • Don't use directory commands!! Prefer `target_link_options` to `add_link_options` in CMake 3.13+. See my answer for more details. – Alex Reinking Jan 21 '21 at 00:44
  • @AlexReinking, thank for your comment! I've updated the answer and added this information. – Gluttton Jan 21 '21 at 13:15
3

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
  • 6,342
  • 2
  • 28
  • 47
2

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

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
onqtam
  • 3,988
  • 1
  • 23
  • 43
2

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
  • 28,342
  • 21
  • 95
  • 123
Mike Spear
  • 626
  • 7
  • 12
1

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
  • 28,342
  • 21
  • 95
  • 123
Petrus Kambala
  • 82
  • 1
  • 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