36

You can set the COMPILE_OPTIONS on an INTERFACE library (foo) and those COMPILE_OPTIONS will also be used by the users of foo.

add_library(foo INTERFACE)
target_link_libraries(foo INTERFACE foo_1)
target_compile_options(foo INTERFACE "-DSOME_DEFINE")
add_executable(exe exe.cpp)
target_link_libraries(exe foo)

Is it possible to do something similar for LINK_FLAGS ?

Groleo
  • 584
  • 1
  • 4
  • 7
  • Can you share a particular use case? –  Jul 30 '14 at 19:08
  • 1
    @ruslo, sure. For example, a library that needs LD flag "--allow-multiple-definitions" or on windows /FORCE:MULTIPLE. – Groleo Jul 30 '14 at 19:16
  • Possible duplicate of [How do I add a linker or compile flag in a CMake file?](https://stackoverflow.com/questions/11783932/how-do-i-add-a-linker-or-compile-flag-in-a-cmake-file) – vitaut Dec 16 '18 at 04:39

3 Answers3

48

CMake has a target_link_options starting from version 3.13 that does exactly that.

target_link_options(<target> [BEFORE]
  <INTERFACE|PUBLIC|PRIVATE> [items1...]
  [<INTERFACE|PUBLIC|PRIVATE> [items2...] ...])

target_link_options documentation

João Neto
  • 1,732
  • 17
  • 28
  • Using 3.6.4111459 with AndroidStudio, I get: Unknown CMake command "target_link_options". – Bram Sep 17 '19 at 17:26
  • 1
    @Bram You need to upgrade the version to at least version 3.13. Check the following question: https://stackoverflow.com/questions/51471908/upgrade-cmake-version-installed-with-android-studio-ubuntu-18-04 – João Neto Sep 17 '19 at 19:58
  • As per documentation, this command cannot be used to add options for static library targets, since they do not use a linker. To add archiver or MSVC librarian flags, see the STATIC_LIBRARY_OPTIONS target property. – tmt Dec 23 '22 at 15:12
27

According to the documentation there is no such property as INTERFACE_LINK_OPTIONS or something. Probably because INTERFACE_* properties used to describe how to use target (like avoiding violation of ODR rule or undefined references) and such options like --allow-multiple-definitions is not related to usage of a specific library (IMHO it's an indication of an error).

Anyway, for compiler like gcc you can use target_link_libraries to add linker flags too:

target_link_libraries(foo INTERFACE "-Wl,--allow-multiple-definition")

But I don't know how to do something like that for visual studio.

  • that worked. For windows* I expect that replacing "-Wl,--allow-multiple-definition" with "/FORCE:multiple". * http://msdn.microsoft.com/en-us/library/70abkas3.aspx – Groleo Jul 30 '14 at 20:12
  • 1
    I think there's another use case for this. For example using LD:--undefined=symbol , or --allow-shlib-undefined. I think it's not added to CMake since it's easier to have a compiler agnostic (defines) instead of mapping the linker options to INTERFACE_* properties ? – Groleo Jul 30 '14 at 20:32
  • 3
    @ruslo: use `-` instead of `/` for cl.exe linker options, cl.exe accepts both and starting with `-` avoids the misinterpretation as library name – mgr Aug 09 '17 at 14:27
  • `target_link_libraries(foo PRIVATE optimized -LTCG optimized -INCREMENTAL:NO)` works with `Microsoft Visual Studio Community 2017, Version 15.6.4` and CMake 3.11 – Liviu Mar 29 '18 at 12:59
15

Edit: Modern CMake now provides target_link_options(), as answered here.


You could try something like this:

add_library(foo INTERFACE)
target_link_libraries(foo INTERFACE foo_1)
target_compile_options(foo INTERFACE "-DSOME_DEFINE")
add_executable(exe exe.cpp)
target_link_libraries(exe foo)

set_target_properties(foo PROPERTIES LINK_FLAGS "My lib link flags")
set_target_properties(exe PROPERTIES LINK_FLAGS "My exe link flags")
Kevin
  • 16,549
  • 8
  • 60
  • 74