[CMake] Append an option to CMAKE_C_FLAGS_DEBUG

Michael Wild themiwi at gmail.com
Tue Apr 20 04:55:51 EDT 2010


On 20. Apr, 2010, at 10:31 , Mathieu Dubois wrote:

> Hello,
> 
> I'm a new CMake user under Linux and I like it.
> 
> I have a simple question concerning CMAKE_C_FLAGS_DEBUG.
> 
> I have made a library and I would like it to print more information when compiled when CMAKE_BUILD_TYPE=Debug.
> I think that the best way is to define a DEBUG preprocessor variable and modify CMAKE_C_FLAGS_DEBUG.
> 
> The problem is that CMake sets CMAKE_C_FLAGS_DEBUG to "-g" which is mandatory.
> 
> My question is: how can I "append" the option "-DDEBUG" to the CMAKE_C_FLAGS_DEBUG variable?
> 
> I have tried
> set (CMAKE_C_FLAGS_DEBUG "-DDEBUG" ${CMAKE_C_FLAGS_DEBUG})
> without effect.
> 
> Thanks in advance,
> Mathieu

The usual way is to do it just the other way around, and that's also how CMake does it. Instead of defining DEBUG for debug builds, it defines NDEBUG for release builds. So, in your code do:

#ifndef NDEBUG
printf("THIS IS DEBUG INFORMATION\n");
#endif


Of course, if you insist on DEBUG you can do the following in your top-level CMakeLists.txt file:

set_directory_properties(PROPERTIES COMPILE_DEFINITIONS_DEBUG DEBUG=1)


HTH
Michael


More information about the CMake mailing list