Linking against a debug version of a library with CMake

c++, cmake, dll, linker

Solution

You should not rename the file manually. Use CMake's `CMAKE_DEBUG_POSTFIX` variable or the `DEBUG_POSTFIX` target property instead:

add_library(myLib SHARED ${SOURCES})
set_target_properties(mylib PROPERTIES DEBUG_POSTFIX "d")

[...]
add_executable(myApp WIN32 ${SOURCES})
target_link_libraries(myApp myLib)

Problem

I've got some problems with linking against a debug version of my lib. I use CMake to make a library: ``` project(myLib) ... add_library(myLib SHARED ${SOURCES}) ``` I launch the build two times to get a release and a debug version of my lib. Then I add 'd' suffix to the name of the debug lib and have `myLib.dll` and `myLibd.dll`. In my app I explicitly link against the debug dll: ``` project(myApp) add_executable(myApp WIN32 ${SOURCES}) target_link_libraries(myApp myLibd.dll) ``` The build finishes successfully, but when I open the resulting exe file with Dependency Walker I get an unresolved dependency to `myLib.dll` file, even though the debug version (`myLibd.dll`) is located in the same folder. So, why does my app try to use the release version of my lib at runtime? And how do I properly link against the debug version?

Original source

Related problems