gcc 中将非保留标识符作为内置宏的原因是什么?

发布于 2024-12-23 11:10:20 字数 1070 浏览 3 评论 0原文

今天,我偶然发现了一个相当有趣的编译器错误:

int main() {
  int const unix = 0; // error-line
  return unix;
}

使用 gcc 4.3.2 给出以下消息(是的,古老的......):

error: expected unqualified-id before numeric constant

这绝对是相当令人困惑的。

幸运的是,clang (3.0) 有点帮助(像往常一样):

error: expected unqualified-id
  int const unix = 0
            ^
<built-in>:127:14: note: expanded from:
#define unix 1
             ^

我当然没想到 unix 既不是用大写写的,也不是以下划线开头的,它是一个宏,尤其是一个内置的-合而为一。

我检查了 gcc 中的预定义宏,有 2 个(在我的平台上)使用“未保留”符号:

$ g++ -E -dM - < /dev/null | grep -v _
#define unix 1
#define linux 1

所有其他都是带有前导下划线的“行为良好”宏,使用传统的保留标识符,示例:(

#define __linux 1
#define __linux__ 1
#define __gnu_linux__ 1

#define __unix__ 1
#define __unix 1

#define __CHAR_BIT__ 8
#define __x86_64 1
#define __amd64 1
#define _LP64 1

这是一团糟,似乎没有任何特定的顺序...)

此外,还有很多“相似”的符号,所以我猜测存在向后兼容性的问题...

那么,unix在哪里和linux 宏来自于?

Today I stumbled upon a rather interesting compiler error:

int main() {
  int const unix = 0; // error-line
  return unix;
}

Gives the following message with gcc 4.3.2 (yes, ancient...):

error: expected unqualified-id before numeric constant

which is definitely quite confusing.

Fortunately, clang (3.0) is a little more helpful (as usual):

error: expected unqualified-id
  int const unix = 0
            ^
<built-in>:127:14: note: expanded from:
#define unix 1
             ^

I certainly did not expect unix, which is neither written in upper-case nor begin with underscore to be a macro, especially a built-in one.

I checked the predefined macros in gcc and there are 2 (on my platform) that use "unreserved" symbols:

$ g++ -E -dM - < /dev/null | grep -v _
#define unix 1
#define linux 1

All the others are "well-behaved" macros with leading underscores, using the traditional reserved identifiers, sample:

#define __linux 1
#define __linux__ 1
#define __gnu_linux__ 1

#define __unix__ 1
#define __unix 1

#define __CHAR_BIT__ 8
#define __x86_64 1
#define __amd64 1
#define _LP64 1

(it's a mess and there does not seem to be any particular order...)

Furthermore, there are lots of "similar" symbols, so I guess there is an issue of backward compatibility...

So, where do the unix and linux macros come from ?

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

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

发布评论

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

评论(1

三生一梦 2024-12-30 11:10:20

默认情况下,gcc 并不完全符合任何 C 标准。

使用 -ansi-std=c99-std=c1x 调用它,并且 unix 不会预定义的。 (-std=c1x 可能会成为未来更新的 gcc 版本中成为 -std=c11。)

有点令人困惑的是,这是在 GNU 预处理器的单独手册中记录的,而不是在 gcc 手册中。

引用 GNU 预处理器文档(info cpp,版本 4.5):

C 标准要求所有系统特定宏都必须是
“保留的命名空间”。所有以两个下划线开头的名称,
或下划线和大写字母,为编译器保留
和图书馆,随心所欲地使用。然而,历史上
系统特定宏的名称没有特殊前缀;为了
例如,在 Unix 系统上定义“unix”是很常见的。为了
所有此类宏,GCC 提供了带有两个下划线的并行宏
添加在开头和结尾。如果定义了“unix”,
`__unix__' 也将被定义。永远不会超过两个
下划线; `_mips' 的平行项是 `__mips__'。

当“-ansi”选项或任何要求严格的“-std”选项时
一致性,被赋予编译器,所有系统特定的
保留命名空间之外的预定义宏将被抑制。这
保留命名空间内的并行宏保持定义。

我们正在慢慢淘汰所有超出范围的预定义宏
保留的命名空间。你永远不应该在新程序中使用它们,我们
鼓励您更正旧代码以使用并行宏
每当你找到它时。我们不建议您使用系统特定的
保留命名空间中的宏。最好是在
从长远来看,使用工具专门检查您需要的功能
例如“autoconf”。

当前版本的手册是 这里

gcc does not fully conform to any C standard by default.

Invoke it with -ansi, -std=c99, or -std=c1x and unix won't be predefined. (-std=c1x will probably become became -std=c11 in a future more recent gcc release.)

It's a bit confusing that this is documented in the separate manual for the GNU preprocessor, not in the gcc manual.

Quoting the GNU preprocessor documentation (info cpp, version 4.5):

The C standard requires that all system-specific macros be part of
the "reserved namespace". All names which begin with two underscores,
or an underscore and a capital letter, are reserved for the compiler
and library to use as they wish. However, historically
system-specific macros have had names with no special prefix; for
instance, it is common to find `unix' defined on Unix systems. For
all such macros, GCC provides a parallel macro with two underscores
added at the beginning and the end. If `unix' is defined,
`__unix__' will be defined too. There will never be more than two
underscores; the parallel of `_mips' is `__mips__'.

When the `-ansi' option, or any `-std' option that requests strict
conformance, is given to the compiler, all the system-specific
predefined macros outside the reserved namespace are suppressed. The
parallel macros, inside the reserved namespace, remain defined.

We are slowly phasing out all predefined macros which are outside the
reserved namespace. You should never use them in new programs, and we
encourage you to correct older code to use the parallel macros
whenever you find it. We don't recommend you use the system-specific
macros that are in the reserved namespace, either. It is better in
the long run to check specifically for features you need, using a tool
such as `autoconf'.

The current version of the manual is here.

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