tags:

views:

81

answers:

2

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

A: 
if(CMAKE_BUILD_TYPE strequal "Debug")
  add_library(target debuglibraries)
elseif(CMAKE_BUILD_TYPE strequal "Release")
  add_library(target releaselibraries)
elseif(CMAKE_BUILD_TYPE strequal "RelWithDebInfo")
  add_library(target debuglibraries)
elseif(CMAKE_BUILD_TYPE strequal "MinSizeRel")
  add_library(target releaselibraries)
endif(CMAKE_BUILD_TYPE strequal "Debug")
greyfade
I thought that, but the docs say CMAKE_BUILD_TYPE is only valid for single generators like Makefiles, so it wouldn't work for Visual Studio.
gct
The doc for `CMAKE_CFG_INTDIR` says CMake has no way of knowing what VS will pick for a build type. Maybe the CMake ML will have ideas.
greyfade
+4  A: 

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)
Kassini
I do this all the time, even more compactly on one line:target_link_libraries(MyEXE debug 3PDebugLib optimized 3PReleaseLib)
Christopher Bruns