Search code examples
cmakelinker-flags

CMake set per configuration linker flag


As the title states, is there a 'neat' way to set per configuration linker flag? It is not possible for me to use target_link_options() since I'm trying to link into a static library. Ideally, what I would want to achieve is to set Visual Studio's Librarian -> Additional Dependencies and Additional Library Directories and achieve the same result in XCode as well.

Here is what I've achieved so far:

set(LIB_LINKS_RUNTIME
    ${Vulkan_LIBRARIES}
    ${CMAKE_SOURCE_DIR}/bin/$<CONFIGURATION>/SDL2maind.lib
    ${CMAKE_SOURCE_DIR}/bin/$<CONFIGURATION>/SDL2d.lib
    ${CMAKE_SOURCE_DIR}/bin/$<CONFIGURATION>/imgui.lib
)
foreach(lib ${LIB_LINKS_RUNTIME})
    set (CMAKE_STATIC_LINKER_FLAGS "${CMAKE_STATIC_LINKER_FLAGS} ${lib}")
endforeach()

Solution

  • According to https://cmake.org/cmake/help/latest/prop_tgt/STATIC_LIBRARY_OPTIONS.html STATIC_LIBRARY_OPTIONS property does the trick

    set_property(TARGET Runtime PROPERTY STATIC_LIBRARY_OPTIONS ${LIB_LINKS_RUNTIME})