_GNU_SOURCE和__USE_GNU的差别

本文转载至:http://mnstory.net/2015/03/_gnu_source-vs-__use_gnu/#more-371

c-cpp.c:

用gcc编译不过,用g++可以:
$ g++ c-cpp.c
$ gcc c-cpp.c
c-cpp.c: In function ‘main’:
c-cpp.c:12:17: error: ‘REG_R8’ undeclared (first use in this function)
c-cpp.c:12:17: note: each undeclared identifier is reported only once for each function it appears in

其预处理上有差别:
$ gcc siginfo.c -E >gcc.c
$ g++ siginfo.c -E >g++.c

在/usr/include/x86_64-linux-gnu/sys/ucontext.h 文件里,定义了:

经过预处理,gcc.c显示为:

g++.c显示为:

很明显,gcc版本的,没有enum,所有gcc编译不过。

换一下:

看下在gcc下,两者的预处理差别:
/usr/include/features.h 里面,首先会:

然后根据_GNU_SOURCE来定义:

__USE_GNU 不是开放给用户用的,features.h里面会修改,但 _GNU_SOURCE 不会,它才是给用户用的
看features.h里的注释:
These are defined by the user (or the compiler) to specify the desired environment:
_GNU_SOURCE All of the above, plus GNU extensions.

These are defined by this file and are used by the header files to decide what to declare or define:
__USE_GNU Define GNU extensions.

修改方法,在代码最开始,添加:

或者,直接在编译选项里定义。
而gcc和g++的默认定义也有区别,因为g++默认定义了_GNU_SOURCE,所以g++可以编译过而gcc编译不过。

在这里 http://stackoverflow.com/questions/5679267/how-to-resolve-reg-eip-undeclared-first-use-in-this-function-error-on-linux-3 也搜索到答案,但是之前没有太留意。
I believe you should either have #define _GNU_SOURCE as the first line of your source file, or better put -D_GNU_SOURCE in your CFLAGS (on the command line). Then make sure you include and .


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值