hookenz
hookenz

Reputation: 38947

How do I detect a debug build in my c source files with cmake?

I have this set in my CMakeLists.txt file.

SET (CMAKE_BUILD_TYPE "Debug")

However, this is not working in my C source files

#if defined(DEBUG)
// not getting here
#else
// getting here instead
#endif

What symbol if any are defined by setting CMAKE_BUILD_TYPE to Debug ?

Upvotes: 2

Views: 1665

Answers (1)

hookenz
hookenz

Reputation: 38947

Found the answer. It's definately a trap for those new to using cmake.

It seems that changing the build type in CMakeLists.txt will not change the build type if you've built it previously with another build type. The reason is because of it's cache.

Seeing as I am building out of source. i.e. I have a separate Build directory inside my source tree. I normally would run just "cmake .."

So clearing the Build tree and running "cmake .." again fixed it. Now DEBUG is being defined for my source files and I can verify it with make VERBOSE=1

This link provides further detail and other options:

Understanding why CMAKE_BUILD_TYPE cannot be set

Upvotes: 4

Related Questions