9

I am trying to differentiate between a debug and release build.

If a debug build is in progress I want to install myLibd in /usr/local/lib. If a release build is in progress I want to install myLib in /usr/local/lib.

Here is my approach

IF(CMAKE_BUILD_TYPE MATCHES RELEASE)
    SET(LIB_NAME myLib) 
ELSE()
    SET(LIB_NAME myLibd) 
ENDIF(CMAKE_BUILD_TYPE MATCHES RELEASE)

ADD_LIBRARY(${LIB_NAME} ${Source_files} ${Header_files})
INSTALL(TARGETS ${LIB_NAME} DESTINATION /usr/local/lib)

However, the target name is in both cases (CMAKE_BUILD_TYPE=Debug or Release) always myLibd. What is the problem here?

Anonymous
  • 4,617
  • 9
  • 48
  • 61
  • possible duplicate of [CMake : Changing name of Visual Studio and Xcode exectuables depending on configuration in a project generated by CMake](http://stackoverflow.com/questions/6561754/cmake-changing-name-of-visual-studio-and-xcode-exectuables-depending-on-config) – André Dec 18 '14 at 11:36

2 Answers2

12

Solution

Set CMAKE_DEBUG_POSTFIX variable:

if(NOT CMAKE_DEBUG_POSTFIX)
  set(CMAKE_DEBUG_POSTFIX d)
endif()

Details

What is the problem here?

  • You need to use if(CMAKE_BUILD_TYPE MATCHES Release) instead of if(CMAKE_BUILD_TYPE MATCHES RELEASE)
  • Probably you need to clean build directory
  • Note that if(CMAKE_BUILD_TYPE...) approach will not work for multi-configuration generators
Community
  • 1
  • 1
  • btw, when linking debug/release configuration, this [link](https://stackoverflow.com/questions/2209929/linking-different-libraries-for-debug-and-release-builds-in-cmake-on-windows) helps – ZFY Sep 16 '18 at 15:17
2
set_property(TARGET ${LIB_NAME} PROPERTY DBG_POSTFIX d)

Reference

maxint
  • 1,005
  • 8
  • 12
  • It's `DEBUG_POSTFIX`, because the name of `` in the variable definition is `Debug` then capitalized for variable name purposes. – Meteorhead Jul 12 '22 at 08:19