target_link_libraries is probably the most useful and confusing command in CMake. It takes a target ( another ) and adds a dependency if a target is given. If no target of that name ( one ) exists, then it adds a link to a library called one on your path (hence the name of the command).
Let's start by adding the library's directory as a subdirectory to our myapp project. add_subdirectory makes the library test defined in libtestproject available to the build. In target_link_libraries we tell CMake to link it to our executable. CMake will make sure to first build test before linking it to myapp.
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
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.
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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With