Does cmake have something like target_link_options?

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

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.


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

Tags:

Cmake