#ifdef DEBUG 与 CMake 独立于平台

2021-12-26 00:00:00 c cmake c++

我正在使用 CMake 在 Windows (Visual Studio) 以及 Linux 机器 (gcc) 上构建我的项目.我想将一些代码标记为仅调试",例如

I am using CMake for building my projects on Windows (Visual Studio) as well as on Linux machines(gcc). I'd like to mark some code as "debugging only", like with

#ifdef DEBUG
//some logging here
#endif

问题是:CMake调试"构建类型的所有平台上都有哪些编译器定义?DEBUG 好像不存在.(我只想在构建类型为 Debug 时进行日志记录或其他任何操作.)

The question is: what compiler definition is available on all platforms in the CMake "Debug" build type? DEBUG seems not to exist. (I want to have the logging or whatever only when the build type is Debug.)

推荐答案

CMake 默认将 -DNDEBUG 添加到 CMAKE_C_FLAGS_{RELEASE, MINSIZEREL} 中.因此,您可以使用 #ifndef NDEBUG.

CMake adds -DNDEBUG to the CMAKE_C_FLAGS_{RELEASE, MINSIZEREL} by default. So, you can use #ifndef NDEBUG.

相关文章