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 ?

3

3 Answers

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

3

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.

4

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

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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.