c - 为什么 stdlib.h 充满了外部函数原型(prototype)和 gcc 对此的差异

标签 c extern linkage

我知道以下 C 标准摘录中介绍的 C 链接规则:

1/ An identifier declared in different scopes or in the same scope more than once can be made to refer to the same object or function by a process called linkage. There are three kinds of linkage: external, internal, and none.

2/ In the set of translation units and libraries that constitutes an entire program, each declaration of a particular identifier with external linkage denotes the same object or function. Within one translation unit, each declaration of an identifier with internal linkage denotes the same object or function. Each declaration of an identifier with no linkage denotes a unique entity.

3/ If the declaration of a file scope identifier for an object or a function contains the storage-class specifier static, the identifier has internal linkage.

4/ For an identifier declared with the storage-class specifier extern in a scope in which a prior declaration of that identifier is visible, if the prior declaration specifies internal or external linkage, the linkage of the identifier at the later declaration is the same as the linkage specified at the prior declaration. If no prior declaration is visible, or if the prior declaration specifies no linkage, then the identifier has external linkage.

5/ If the declaration of an identifier for a function has no storage-class specifier, its linkage is determined exactly as if it were declared with the storage-class specifier extern. If the declaration of an identifier for an object has file scope and no storage-class specifier, its linkage is external.

6/ The following identifiers have no linkage: an identifier declared to be anything other than an object or a function; an identifier declared to be a function parameter; a block scope identifier for an object declared without the storage-class specifier extern.

7/ If, within a translation unit, the same identifier appears with both internal and external linkage, the behavior is undefined.

我知道 extern 关键字在函数声明之前是可选的,因为它们默认是外部的,但在 stdlib.h 中有一些函数原型(prototype)前面有 extern,例如:

extern void qsort (void *__base, size_t __nmemb, size_t __size,
           __compar_fn_t __compar) __nonnull ((1, 4));

另外,为什么 gcc 在涉及函数和变量时会以不同方式处理第 7 点中描述的情况。在此示例中,函数 foo 和变量 d 都在内部和外部范围内定义,但只有变量定义会引发错误:

static int foo(void); 
int foo(void); /* legal */

static double d;
double d; /* illegal */

最佳答案

在函数声明之前可以自由放置或不放置extern,所以在某处找到它也就不足为奇了。关于第二个问题:

C11 草案 (n1570.pdf) 在第 159 页有与暂定定义相关的示例:

static int i5; // tentative definition, internal linkage
// ...
int i5; // 6.2.2 renders undefined, linkage disagreement
extern int i5; // refers to previous, internal linkage

6.2.2 是您发布的内容。因此,在这种情况下它不起作用,因为有两个具有不同链接的暂定定义,因此存在 p.7 违规行为。另一方面,它适用于外部说明符(如示例中的 foo 函数),因为 p.4 是强制执行的 - 后面的声明引用第一个声明中定义的链接。换句话说,带有变量的 case 不起作用,因为它们是对象并且涉及暂定定义规则。至少标准包含明确的例子,清楚地解释了委员会想说的话。

关于c - 为什么 stdlib.h 充满了外部函数原型(prototype)和 gcc 对此的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19198857/

相关文章:

有人可以详细解释 C 中的链接概念吗

c - 为什么 C 语言的控制台图形这么慢?

c - 应该如何正确使用 wctype.h 函数?

c - 关于二叉树的一些事情

c++ - 重构大型 case 语句、externs、static locals

c++ - 如何在命名空间中初始化外部变量

c - AIX loadquery() 返回值解释(也是 64 位模式下的 SEGV)

c# - P/调用外部方法从 C# 到 C++

c - 创建共享对象时不能使用针对 `.text' 的重定位 R_X86_64_32S

c++ - 非类型(引用)模板参数和链接