Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linking against a debug version of a library with CMake

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?

like image 551
hank Avatar asked Jul 25 '13 17:07

hank


People also ask

Does CMake default to debug or release?

It will now default to using a debug build if the source directory is a git clone, or a release build if not. It is also quite easy to customize its behavior according to the preferences of your project.

What does target link libraries do in CMake?

Specify libraries or flags to use when linking a given target and/or its dependents. Usage requirements from linked library targets will be propagated. Usage requirements of a target's dependencies affect compilation of its own sources.

How do I debug CMake?

First, switch to CMake Targets View in the Solution Explorer window. Then, right-click on an executable and select Debug. This command automatically starts debugging the selected target based on your active configuration.


1 Answers

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)
like image 85
ComicSansMS Avatar answered Sep 19 '22 21:09

ComicSansMS