Groleo
Groleo

Reputation: 616

Does cmake have something like target_link_options?

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 ?

Upvotes: 36

Views: 44900

Answers (3)

Kishore Jonnalagadda
Kishore Jonnalagadda

Reputation: 375

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

Upvotes: 15

João Neto
João Neto

Reputation: 1840

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

Upvotes: 48

user2288008
user2288008

Reputation:

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.

Upvotes: 27

Related Questions