C++ 确定是否在不定义预处理器符号的情况下使用调试符号进行编译
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5223299/
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
C++ determine if compiling with debug symbols without defining a preprocessor symbol
提问by Steven Lu
I have been using something like this:
我一直在使用这样的东西:
int main(int argc, char *argv[])
{
#ifdef DEBUG
printf("RUNNING DEBUG BUILD");
#else
printf("Running... this is a release build.");
#endif
...
However this requires me to compile with -DDEBUG for the debug build. Does GCC give me some way for me to determine when I am compiling with debug symbols (-g flag) such as defining its own preprocessor macro that I can check for?
但是,这需要我使用 -DDEBUG 进行编译以进行调试构建。GCC 是否为我提供了某种方式来确定我何时使用调试符号(-g 标志)进行编译,例如定义我可以检查的自己的预处理器宏?
采纳答案by JoeSlav
Answer is no. Usually these macros (DEBUG, NDEBUG, _DEBUG) are set by the IDE/make system depending on which configuration (debug/release) you have active. I think these answers can be of help:
答案是否定的。通常,这些宏(DEBUG、NDEBUG、_DEBUG)由 IDE/make 系统设置,具体取决于您激活的配置(调试/发布)。我认为这些答案可能会有所帮助:
C #define macro for debug printing
回答by Steven Lu
I think the answer that I was looking for was essentially what Adam posted as a comment, which is:
我认为我正在寻找的答案基本上是亚当作为评论发表的,即:
The compiler's job does not include preprocessing, and in fact the compiler will choke on any preprocessor switches not handled by the preprocessor that make their way into code.
编译器的工作不包括预处理,实际上编译器会阻塞任何预处理器未处理的、进入代码的预处理器开关。
So, because the way to branch code has to leverage the preprocessor, it means by the time the compiler getsany code it's already one or the other (debug code or release code), so it's impossible for me to do what my question asks at this stage (after preprocessor).
因此,因为分支代码的方式必须利用预处理器,这意味着当编译器获得任何代码时,它已经是一个或另一个(调试代码或发布代码),所以我不可能做我的问题这个阶段(预处理器之后)。
So it is a direct consequence of the preprocessor being designed as a separate process for feeding the code through.
因此,这是将预处理器设计为用于馈送代码的单独过程的直接结果。