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?

1
  • 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 Jul 2, 2014 at 14:06

7 Answers 7

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.

5
  • 5
    Note that CMAKE_SHARED_LINKER_FLAGS is for dynamic libraries. For static libraries use CMAKE_STATIC_LINKER_FLAGS instead. Jul 20, 2017 at 2:47
  • 1
    Don't use directory commands!! Prefer target_link_options to add_link_options in CMake 3.13+. See my answer for more details. Jan 21, 2021 at 0:44
  • @AlexReinking, thank for your comment! I've updated the answer and added this information.
    – Gluttton
    Jan 21, 2021 at 13:15
  • What about LINK_WHAT_YOU_USE?
    – Dan M.
    Dec 2, 2021 at 17:15
  • Wl "-Wl,--as-needed" means to pass --as-needed directly to linker
    – isudfv
    Aug 2, 2022 at 8:19
17

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

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.

1
  • 4
    DO NOT USE THIS VARIABLE THAT WAY. The documentation 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." Jan 28, 2021 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")

2

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.