76

I set the CFLAGS in CMake by CMAKE_C_FLAGS. Is something like this to set LDFLAGS?

jww
  • 97,681
  • 90
  • 411
  • 885
hich9n
  • 1,578
  • 2
  • 15
  • 32

5 Answers5

61

It depends a bit on what you want:

A) If you want to specify which libraries to link to, you can use find_library to find libs and then use link_directories and target_link_libraries to.

Of course, it is often worth the effort to write a good find_package script, which nicely adds "imported" libraries with add_library( YourLib IMPORTED ) with correct locations, and platform/build specific pre- and suffixes. You can then simply refer to 'YourLib' and use target_link_libraries.

B) If you wish to specify particular linker-flags, e.g. '-mthreads' or '-Wl,--export-all-symbols' with MinGW-GCC, you can use CMAKE_EXE_LINKER_FLAGS. There are also two similar but undocumented flags for modules, shared or static libraries:

CMAKE_MODULE_LINKER_FLAGS
CMAKE_SHARED_LINKER_FLAGS
CMAKE_STATIC_LINKER_FLAGS
starball
  • 20,030
  • 7
  • 43
  • 238
André
  • 18,348
  • 6
  • 60
  • 74
  • 4
    And CMAKE_STATIC_LINKER_FLAGS for static library: http://www.cmake.org/cmake/help/v3.0/variable/CMAKE_STATIC_LINKER_FLAGS.html – Cofyc Jun 18 '15 at 09:43
  • Just a link to the recent CMake3.0 description: [CMAKE_EXE_LINKER_FLAGS](https://cmake.org/cmake/help/v3.0/variable/CMAKE_EXE_LINKER_FLAGS.html). Note that the MODULE/SHARED/STATIC flags are already documented (though pretty vaguely). – yman Nov 04 '16 at 14:56
  • What is the Module flag for? – CMCDragonkai Sep 04 '17 at 06:40
  • @CMCDragonkai I think it is used for clang module library in macOS. – merito Dec 14 '17 at 07:04
  • How do you use the defines? `CMAKE_MODULE_LINKER_FLAGS += "-Mprogram.mapfile"` fails. – jww Nov 09 '18 at 21:11
  • cmake has recently added add_link_options(), similar to add_compile_options() - see https://cmake.org/cmake/help/v3.13/command/add_link_options.html – Bruce Adams Dec 07 '18 at 14:46
26

Look at:

CMAKE_EXE_LINKER_FLAGS
CMAKE_MODULE_LINKER_FLAGS
CMAKE_SHARED_LINKER_FLAGS
CMAKE_STATIC_LINKER_FLAGS
thiagowfx
  • 4,832
  • 6
  • 37
  • 51
tibur
  • 11,531
  • 2
  • 37
  • 39
13

If you want to add a flag to every link, e.g. -fsanitize=address then I would not recommend using CMAKE_*_LINKER_FLAGS. Even with them all set it still doesn't use the flag when linking a framework on OSX, and maybe in other situations. Instead use link_libraries():

add_compile_options("-fsanitize=address")
link_libraries("-fsanitize=address")

This works for everything.

Timmmm
  • 88,195
  • 71
  • 364
  • 509
8

You can specify linker flags in target_link_libraries.

Simon
  • 31,675
  • 9
  • 80
  • 92
4

For linking against libraries see Andre's answer.

For linker flags - the following 4 CMake variables:

CMAKE_EXE_LINKER_FLAGS
CMAKE_MODULE_LINKER_FLAGS
CMAKE_SHARED_LINKER_FLAGS
CMAKE_STATIC_LINKER_FLAGS

can be easily manipulated for different configs (debug, release...) with the ucm_add_linker_flags macro of ucm

Community
  • 1
  • 1
onqtam
  • 4,356
  • 2
  • 28
  • 50