C++确定是否使用调试符号进行编译而不定义预处理器符号

发布于 2024-10-20 20:29:15 字数 292 浏览 2 评论 0原文

我一直在使用这样的东西:

int main(int argc, char *argv[])
{

#ifdef DEBUG
    printf("RUNNING DEBUG BUILD");
#else
    printf("Running... this is a release build.");
#endif
...

但这需要我使用 -DDEBUG 进行编译以进行调试构建。 GCC 是否为我提供了某种方法来确定何时使用调试符号(-g 标志)进行编译,例如定义我可以检查的自己的预处理器宏?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

完美的未来在梦里 2024-10-27 20:29:15

答案是否定的。通常这些宏(DEBUG、NDEBUG、_DEBUG)由 IDE/make 系统根据您激活的配置(调试/发布)进行设置。我认为这些答案可能会有所帮助:

C #define macro for debug print

-DNDEBUG 通常来自哪里?

_DEBUG 与 NDEBUG

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:

C #define macro for debug printing

Where does the -DNDEBUG normally come from?

_DEBUG vs NDEBUG

各空 2024-10-27 20:29:15

我认为我正在寻找的答案本质上是 Adam 作为评论发布的内容,即:

编译器的工作不包括预处理,事实上,编译器会因预处理器未处理的任何预处理器开关而卡住,这些开关进入代码。

因此,因为分支代码的方式必须利用预处理器,这意味着当编译器获取任何代码时,它已经是其中之一(调试代码或发布代码),所以这对我来说是不可能的做我的问题在这个阶段所要求的事情(在预处理器之后)。

因此,这是预处理器被设计为用于提供代码的单独进程的直接结果。

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 gets any 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文