Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake variable or property to discern betwen debug and release builds

I want to set a CMake variable differently for debug and release builds. I have tried to use CMAKE_CFG_INTDIR like this:

IF(${CMAKE_CFG_INTDIR} STREQUAL "Debug")
    SET(TESTRUNNER DllPlugInTesterd_dll)
ELSE(${CMAKE_CFG_INTDIR} STREQUAL "Debug")
    SET(TESTRUNNER DllPlugInTester_dll)
ENDIF(${CMAKE_CFG_INTDIR} STREQUAL "Debug")

But this variable evaluates to $(OUTDIR) at the time CMake does its thing.

Is there a CMake variable I can use to discern between debug and release builds, or something along the lines of how TARGET_LINK_LIBRARIES where one can specify debug and optimized libraries?

EDIT: I cannot use CMAKE_BUILD_TYPE since this is only supported by make based generators and I need to get this working with Visual Studio.

like image 979
Torleif Avatar asked Sep 03 '09 08:09

Torleif


People also ask

Is RelWithDebInfo slower than release?

Recently we have cases on Release vs RelWithDebInfo, we compile a shared lib, and found either the performance and the lib file size has a significant difference, Release is much more runtime faster and less file size. So it looks like Release should be used when shipping on production.

What is Cmake_build_type?

Specifies the build type on single-configuration generators (e.g. Makefile Generators or Ninja ). Typical values include Debug , Release , RelWithDebInfo and MinSizeRel , but custom build types can also be defined.

What is RelWithDebInfo?

exerpt: "RelwithDebInfo is quite similar to Release mode. It produces fully optimized code, but also builds the program database, and inserts debug line information to give a debugger a good chance at guessing where in the code you are at any time."

What is the default build type in Cmake?

In this answer, it says Debug is the default cmake build configuration.


1 Answers

You can define your own CMAKE_CFG_INTDIR

IF(NOT CMAKE_CFG_INTDIR)
 SET(CMAKE_CFG_INTDIR "Release")
ENDIF(NOT CMAKE_CFG_INTDIR)

IF(CMAKE_CFG_INTDIR MATCHES "Debug")

...Debug PART...

ELSE(CMAKE_CFG_INTDIR MATCHES "Debug")

...Release PART...

ENDIF(CMAKE_CFG_INTDIR MATCHES "Debug")

Then, when you call cmake add the Definition (-D):

cmake -DCMAKE_CFG_INTDIR=Debug /path/of/your/CMakeLists.txt

For targets, you have two solutions:

First one:

IF(CMAKE_CFG_INTDIR MATCHES "Debug")

TARGET_LINK_LIBRARIES(YOUR_EXE DllPlugInTesterd...)

ELSE(CMAKE_CFG_INTDIR MATCHES "Debug")

TARGET_LINK_LIBRARIES(YOUR_EXE DllPlugInTester...)

ENDIF(CMAKE_CFG_INTDIR MATCHES "Debug")

Second one:

IF(CMAKE_CFG_INTDIR MATCHES "Debug")

FIND_LIBRARY(DLL_PLUGIN DllPlugInTesterd_dll /path/of/your/lib)

ELSE(CMAKE_CFG_INTDIR MATCHES "Debug")

FIND_LIBRARY(DLL_PLUGIN PlugInTester_dll /path/of/your/lib)

ENDIF(CMAKE_CFG_INTDIR MATCHES "Debug")

Then for link

TARGET_LINK_LIBRARIES(YOUR_EXE ${DLL_PLUGIN}...)
like image 145
Nadir SOUALEM Avatar answered Sep 23 '22 07:09

Nadir SOUALEM