当指定-g GCC是否界​​定什么? [英] Does gcc define anything when -g is specified?

查看:155
本文介绍了当指定-g GCC是否界​​定什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不久,我想知道,如果GCC(或g ++。我需要它的 C ,但也很对C ++的好奇)定义的任何特殊符号,如 -g 已启用。可以?如果是这样,是什么符号?

Shortly, I want to know if gcc (or g++. I need it in C, but also am curious about c++) defines any special symbols if -g is enabled. Does it? If so, what symbols?

在搜索过程中我发现:


  • _DEBUG 手动定义(通过手动我的意思是 -D_DEBUG ),并从Visual C程序员采取一种习惯(因为VC定义 _DEBUG 在调试模式下编译时)

  • NDEBUG 是如果不能在调试模式下定义。虽然我发现了几个地方说这句话,我想用我的.c和.cpp文件,并在其中没有或用gcc和g ++没有 -g 没有这样的标志是定义!

  • _DEBUG is defined manually (by manually I mean -D_DEBUG) and is a habit taken from Visual C programmers (because VC defines _DEBUG when compiling in debug mode)
  • NDEBUG is defined if NOT in debug mode. Although I found a few places saying this, I tried with my gcc and g++ in .c and .cpp files and in none of them with or without -g no such symbol was defined!

修改:让我证明为什么我不希望使用非标准符号:

Edit: Let me demonstrate why I don't want to use a non-standard symbol:

想象一下,一个内核模块,做的东西,还提供了包含在其他内核模块的头文件,以便它们可以连接到这一点。

Imagine a kernel module that does something and also provides header files to be included in other kernel modules so that they can connect to this one.

现在作为一个机构,在头文件中的一个我有:

Now as a facility, in one of the header files I have:

#ifdef DEBUG <-- This is what I need
#define LOG(x, ...) printk("Some extra info"x, ##__VA_ARGS__);
#else
#define LOG(x, ...) printk("Without extra info"x, ##__VA_ARGS__);
#endif

请注意,这个名字是不是真的 LOG ,这是一个例子。

Note that the name is not really LOG, that's an example.

现在,我可以使用 DEBUG任何符号我自己,但如果有人,包括我的头,他们可能不会定义符号。当然,我可以告诉他们的方式,来获得在调试模式下头,定义这个符号等,但就是不健全的权利我。

Now, I can use any symbol for DEBUG myself, but then if someone includes my header, they may not define that symbol. Of course I can tell them "by the way, to get the headers in debug mode, define this other symbol", but that just doesn't sound right to me.

我可以定义在报头中的象征,它包括所有的头文件。这样,如果他们有我的头之一,他们得到的调试符号了。现在的问题是,如果他们的的希望在调试模式下进行编译,我的头仍然认为他们是在调试模式。

I could define the symbol in a header and include it in all header files. This way, if they include one of my headers, they get the debug symbol too. The problem now is that, if they don't want to compile in debug mode, my headers still think they are in debug mode.

所以,我认为将是最好是使用时使用 -g 如果有一个定义,一个符号的!

So what I thought would be best was to use a symbol that is defined when -g is used, if there are any!

到目前为止,我得出的结论,我可以做这样的事情:

So far I came to the conclusion that I could do something like this:

how_to_build.h

how_to_build.h

#if !defined(NDEBUG)
#define MY_DEBUG
#endif

用法:

#include "how_to_build.h"

#ifdef MY_DEBUG
// rest of the story

这样, NDEBUG的共同选择还删除我的定义。它仍然要求我告诉他们来定义它,如果他们不想要得到的头在调试模式。

This way, the common option of NDEBUG removes my definition also. It still requires me to tell them to define it if they don't want to get the headers in debug mode.

推荐答案

您可以看到所有宏列表中的gcc / g ++的定义一样,标志的任意组合:

You can see the list of all macros gcc/g++ defines for any combination of flags like that:

$ g++ -E -dD -xc++ /dev/null

例如:

[max@truth ~]$ g++ -E -dD -xc++ /dev/null > a
[max@truth ~]$ g++ -E -dD -xc++ -g -O3 /dev/null > b
[max@truth ~]$ diff a b
1a2
> # 1 "/home/max//"
173c174
< #define __NO_INLINE__ 1
---
> #define __OPTIMIZE__ 1

让我们看看是否 -g 定义了什么:

[max@truth ~]$ g++ -E -dD -xc++ -g /dev/null > c
[max@truth ~]$ diff a c
1a2
> # 1 "/home/max//"

额外preprocessor指令,但 -g 标志没有多余的定义。

这篇关于当指定-g GCC是否界​​定什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆