当指定了-U__GNUC__并且至少有1 #include指令时,预处理器是否会删除属性?对我来说,这似乎是令人惊讶的行为。下面是一个例子:
$ cat foo.c
#include <limits.h>
struct S {
int a;
} __attribute__((__packed__));
$ gcc -E -U__GNUC__ foo.c | tail -3
struct S {
int a;
} ;但是,如果删除#include指令(或者删除-U__GNUC__),那么属性就不会被预处理器剥离,这正是我所期望的。
$ cat foo2.c
struct S {
int a;
} __attribute__((__packed__));
$ gcc -U__GNUC__ -E foo2.c
# 1 "foo2.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "foo2.c"
struct S {
int a;
} __attribute__((__packed__));这是gcc的错误,还是有这种行为的记录?
发布于 2015-03-28 01:43:07
在我的平台上,标准标头间接地包括/usr/include/argp.h。在argp.h中,它说
#ifndef __attribute__
/* This feature is available in gcc versions 2.5 and later. */
# if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5) || __STRICT_ANSI__
# define __attribute__(Spec) /* empty */
# endif
...
#endif也就是说,对于低值的__GNUC__和__STRICT_ANSI__模式,__attribute__被预定义为一个空宏。通过不屈不挠的__GNUC__,您使它在#if上下文中表现得像0一样。因此,上述代码将__attribute__转换为空宏。
https://stackoverflow.com/questions/29312645
复制相似问题