gct
gct

Reputation: 14563

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

So I've got a library I'm compiling and I need to link different third party things in depending on if it's the debug or release build (specifically the release or debug versions of those libraries). Is there an easy way to do this in Cmake?

Edit: I should note I'm using visual studio

Upvotes: 78

Views: 82547

Answers (6)

Kiruahxh
Kiruahxh

Reputation: 2015

In my case, I had to build the external library like this to have both .lib and d.lib in the same directory:

cmake repo -B build -DCMAKE_DEBUG_POSTFIX=d -DCMAKE_MSVC_RUNTIME_LIBRARY="MultiThreaded$<$<CONFIG:Debug>:Debug>" -A x64 -T v143
cmake --build build --config Debug
cmake --install build --prefix dist --config Debug

cmake --build build --config Release
cmake --install build --prefix dist --config Release

Then, simple call to link library went fine:

target_link_libraries(MyEXE nowide::nowide)

This page can help selecting the right version of the library depending on your project configuration: https://cmake.org/cmake/help/latest/prop_tgt/MAP_IMPORTED_CONFIG_CONFIG.html#prop_tgt:MAP_IMPORTED_CONFIG_%3CCONFIG%3E

Upvotes: 2

Mike Willekes
Mike Willekes

Reputation: 6150

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)

Upvotes: 107

Aaron Zhang
Aaron Zhang

Reputation: 141

target_link_libraries with optimize and debug doesn't work for me. I follow the post of Mike Willekes, but release config also import debug library file in visual studio. Then, I use the following cmake code to solving this problem

add_library(BoostLib STATIC IMPORTED)
set_target_properties(BoostLib PROPERTIES 
             IMPORTED_LOCATION_DEBUG ${BoostLibPath}/debug/module1.lib
             IMPORTED_LOCATION_RELEASE ${BoostLibPath}/release/module1.lib)

target_link_libraries(AppTarget BoostLib)

Upvotes: 3

Konrad
Konrad

Reputation: 377

What worked for me was to use $(Configuration) macro in a lib path provided to cmake.

So, assuming libs are stored in separate, correctly named folders, e.g.:

C:\boost\lib\Debug\libfoo.lib
C:\boost\lib\Release\libfoo.lib

You can then call cmake with:

cmake -G "Visual Studio 10 2010" -DBOOST_LIBRARYDIR=C:\boost\lib\$(Configuration)\libfoo.lib

That'll generate .vcxproj with Additional Dependencies including C:\boost\lib\$(Configuration)\libfoo.lib, what is evaluated to either C:\boost\lib\Release\libfoo.lib or C:\boost\lib\Debug\libfoo.lib depending on a chosen Configuration.

Upvotes: 2

akaltar
akaltar

Reputation: 1097

I would like to add a few notes to the previous answers.

If you need to create a list of multiple files you want to link and store that in a cache variable then you need to add the optimized or debug specified before each and every library. This can be especially useful for larger makefiles/projects.

So for example you could do something like this:

set( MyFavLib_LIBRARIES 
    debug debug/module1.lib optimized release/module1.lib
    debug debug/module2.lib optimized release/module2.lib )
target_link_libraries( app ${MyFavLib_LIBRARIES} )

Upvotes: 11

Tarc
Tarc

Reputation: 3312

Somehow the answer from @Mike Willekes got CMake linking in the same target both release and debug for me :(

I only got this working by setting both configurations in one line, as suggested by @sakra in a related question - and doing so for every library that needed to be linked:

target_link_libraries ( app
    debug ${Boost_FILESYSTEM_LIBRARY_DEBUG}
    optimized ${Boost_FILESYSTEM_LIBRARY_RELEASE} )

target_link_libraries ( app
    debug ${Boost_LOG_LIBRARY_DEBUG}
    optimized ${Boost_LOG_LIBRARY_RELEASE} )

target_link_libraries ( app
    debug ${Boost_PROGRAM_OPTIONS_LIBRARY_DEBUG}
    optimized ${Boost_PROGRAM_OPTIONS_LIBRARY_RELEASE} )

# ...

Upvotes: 20

Related Questions