简体   繁体   中英

Set cmake target name for debug and release configuration

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

If a debug build is in progress I want to install myLib d 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 myLib d . What is the problem here?

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
set_property(TARGET ${LIB_NAME} PROPERTY DBG_POSTFIX d)

Reference

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM