C++ #ifdef DEBUG 与 CMake 独立于平台

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8591762/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:50:39  来源:igfitidea点击:

#ifdef DEBUG with CMake independent from platform

c++ccmake

提问by Philipp

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

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

#ifdef DEBUG
//some logging here
#endif

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“调试”构建类型的所有平台上都有哪些编译器定义?DEBUG 好像不存在。(我只想在构建类型为 Debug 时进行日志记录或其他任何操作。)

回答by arrowd

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

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

回答by Lindydancer

I would suggest that you add your own definition. The CMakesymbol CMAKE_C_FLAGS_DEBUGcan contain flags only used in debug mode. For example:

我建议您添加自己的定义。该CMake符号CMAKE_C_FLAGS_DEBUG可以包含仅在调试模式下使用的标志。例如:

C:

C

set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -DMY_DEBUG")

C++:

C++

set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DMY_DEBUG")

In your code you can then write the following:

在您的代码中,您可以编写以下内容:

#ifdef MY_DEBUG
// ...
#endif

(Maybe, you would have to use "/DMY_DEBUG"for visual studio.)

(也许,您将不得不"/DMY_DEBUG"用于视觉工作室。)

回答by jtbr

In CMake >= 2.8, use target_compile_definitions:

在 CMake >= 2.8 中,使用target_compile_definitions

target_compile_definitions(MyTarget PUBLIC "$<$<CONFIG:DEBUG>:DEBUG>")

When compiling in Debug mode, this will define the DEBUG symbol for use in your code. It will work even in IDEs like Visual Studio and Xcode for which cmake generates a single file for all compilation modes.

在调试模式下编译时,这将定义在您的代码中使用的调试符号。它甚至可以在像 Visual Studio 和 Xcode 这样的 IDE 中工作,cmake 会为所有编译模式生成单个文件。

You have to do this for each target [1]. Alternatively you can use add_compile_options(Cmake >= 3.0):

您必须为每个目标执行此操作 [1]。或者,您可以使用add_compile_options(Cmake >= 3.0):

add_compile_options("$<$<CONFIG:DEBUG>:-DDEBUG>")

Note that recent versions of Visual C++ (at least since VS2015) allow either / or - for parameters, so it should work fine across compilers. This command is also useful for other compile options you might like to add ("/O2" in release mode for MSVC or "-O3" for release mode in G++/Clang)

请注意,最新版本的 Visual C++(至少自 VS2015 起)允许 / 或 - 作为参数,因此它应该可以在编译器中正常工作。此命令对于您可能想要添加的其他编译选项也很有用(MSVC 发布模式中的“/O2”或 G++/Clang 发布模式中的“-O3”)

[1] : Note: in CMake >= 3.12 (currently beta) there is also an add_compile_definitionsthat supports generator expressions, which affects all targets.

[1] :注意:在 CMake >= 3.12(目前是测试版)中,还有一个add_compile_definitions支持生成器表达式,它会影响所有目标。