Linking Different Libraries for Debug and Release Builds in Cmake on Windows

Linking different libraries for Debug and Release builds in Cmake on windows?

According to the CMake documentation:

target_link_libraries(<target> [lib1 [lib2 [...]]] [[debug|optimized|general] <lib>] ...)

A "debug", "optimized", or "general"
keyword indicates that the library
immediately following it is to be used
only for the corresponding build
configuration.

So you should be able to do this:

add_executable( MyEXE ${SOURCES})

target_link_libraries( MyEXE debug 3PDebugLib)
target_link_libraries( MyEXE optimized 3PReleaseLib)

Debug and Release Library Linking with CMAKE (VISUAL STUDIO)

There is no problems when your library is a part of the project or you're
importing it using config mode of find_package command (see documentation and example).
In case you can't modify 3rd party so it will produce <package>Config.cmake
(it may not use cmake tool or you don't want to do it) the answer is to emulate
such process:

add_library(foo STATIC IMPORTED)
set_target_properties(foo PROPERTIES IMPORTED_LOCATION_DEBUG "/path/to/foo-d.lib")
set_target_properties(foo PROPERTIES IMPORTED_LOCATION_RELEASE "/path/to/foo.lib")

target_link_libraries(MyEXE foo)

note that unlike "debug"/"optimized" feature such approach is not limited to Debug/Release configs:

set_target_properties(foo PROPERTIES IMPORTED_LOCATION_MINSIZEREL "/path/to/foo-small.lib")

also you've got some goodies like INTERFACE_INCLUDE_DIRECTORIES:

set_target_properties(foo PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "/path/to/foo/includes")

include_directories("/path/to/foo/includes") # this line not needed
target_link_libraries(MyEXE foo) # this command will add "/path/to/foo/includes" for you

and transitive linking:

add_library(boo STATIC IMPORTED)
set_target_properties(boo PROPERTIES IMPORTED_LOCATION_DEBUG "/path/to/boo-d.lib")
set_target_properties(boo PROPERTIES IMPORTED_LOCATION_RELEASE "/path/to/boo.lib")

add_library(foo STATIC IMPORTED)
set_target_properties(foo PROPERTIES IMPORTED_LOCATION_DEBUG "/path/to/foo-d.lib")
set_target_properties(foo PROPERTIES IMPORTED_LOCATION_RELEASE "/path/to/foo.lib")

set_target_properties(foo PROPERTIES INTERFACE_LINK_LIBRARIES boo) # foo depends on boo

target_link_libraries(MyEXE foo) # boo will be linked automatically

Of course you can use regular cmake commands like find_library and find_package(... MODULE) to estimate locations instead of hardcoding them.

CMake - Linking different libraries for Debug and Release builds with variable from find_package

Use a loop:

foreach (_lib ${Cairomm_LIBRARIES})
target_link_libraries(Paint debug ${_lib})
endforeach()

Specifying different libraries for Debug & Release mode

The target_link_libraries command supports "debug" and "optimized" keywords, which indicate that the library immediately following it is to be used only for the corresponding build configuration:

target_link_libraries(MyTarget debug externalLib_d optimized externalLib)

If the debug and release libraries reside in different directories, specify the full path, i.e.:

target_link_libraries(MyTarget debug "debug_dir/externalLib_d" optimized "release_dir/externalLib")

Also see the target_link_libraries command documentation.

Adding different include directory for Debug and Release builds in Cmake?

While target_include_directories itself does not provide the possibility to distinguish between different build types, you can use generator expressions, e.g.:

target_include_directories(MyEXE
PRIVATE
$<$<CONFIG:Debug>:3PDebugLib>
$<$<CONFIG:Release>:3PReleaseLib>
)

Force linking against Python release library in debug mode in Windows/Visual Studio from CMake

It seems that set(Python_FIND_ABI "OFF" "ANY" "ANY") as suggested in the comments by kanstar would be the correct way to do this. However, while Python_FIND_ABI is in CMake master, it hasn't been released yet in the latest version (v3.15.2 as of this writing).

In the meantime, there are solutions dependent on the CMake version.

CMake 3.12 and above

It's possible to link against FindPython's Python_LIBRARY_RELEASE, which isn't meant to be part of the module's public interface, but the variable is set correctly nonetheless.

cmake_minimum_required (VERSION 3.12)
find_package(Python ..<choose your COMPONENTS; refer to FindPython docs>..)
if(WIN32 AND MSVC)
target_link_libraries(my_python_module ${Python_LIBRARY_RELEASE})
endif()

CMake 3.0.4 to 3.11

Thanks to a comment by @Phil, we can expand the answer to include earlier CMake versions which had the FindPythonLibs module that sets the PYTHON_LIBRARY_RELEASE variable instead.

cmake_minimum_required (VERSION 3.0)
find_package(PythonLibs ..<refer to FindPythonLibs docs>..)
if(WIN32 AND MSVC)
target_link_libraries(my_python_module ${PYTHON_LIBRARY_RELEASE})
endif()

Link library based on build configuration [cmake]

The solution is:

SET(LINK_LIBRARY optimized Foo debug Foo_d)
target_link_libraries(MyEXE ${LINK_LIBRARY})

How to build CMake Debug version using different target name for executables?

You are not missing anything. As the documentation says (emphasis mine):

When a non-executable target is created its <CONFIG>_POSTFIX target property is initialized with the value of this variable if it is set.

See: https://cmake.org/cmake/help/latest/variable/CMAKE_CONFIG_POSTFIX.html

By the sounds of it, though, you could manually set the property on your executable target with set_target_properties:

set_target_properties(
target1 ... targetN
PROPERTIES
DEBUG_POSTFIX "${CMAKE_DEBUG_POSTFIX}"
)

Note, however, that it doesn't apply to executables by default because that's rarely what you want. For libraries, it's fairly common to package and deploy debug and release configurations simultaneously. On Windows, for development, it is a requirement. On the other hand, one very rarely needs to deploy debug applications and hence one very rarely wants a postfix applied.



Related Topics



Leave a reply



Submit