对于这个例子,函数 my_func 是否暴露给链接器?

Is the function my_func exposed to the linker or not for this example?

我想进一步了解 my_func() 函数是否暴露给 以下简化示例的链接器。基础知识是哪个优先,前向声明还是声明?

我猜是前向声明,但为什么呢?有人可以解释一下吗,参考。去K&R就好了

static int my_func(void);

<Use my_func here>

int my_func(void)
{
  return 1;
}

例子修改为

会怎样
static int my_func(void);
int my_func(void);

<Use my_func here>

int my_func(void)
{
  return 1;
}

这个

static int my_func(void);

已经是带有内部链接的函数声明。

所以下面的声明

int my_func(void);

是多余的。

这些声明

static int my_func(void);
int my_func(void);

相当于

static int my_func(void);
extern int my_func(void);

来自 C 标准(6.2.2 标识符的链接)

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.

4 For an identifier declared with the storage-class specifier extern in a scope in which a prior declaration of that identifier is visible,31) 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.

因此在这段代码中

static int my_func(void);
int my_func(void);

<Use my_func here>

int my_func(void)
{
  return 1;
}

您有三个相同函数的声明,其中 internal linkage 和声明

int my_func(void);

早说是多余的

表示该功能在其他翻译单元中不可见。或者,如果带有存储 class 说明符 static 的函数声明放在 header 中,则包含 header 的每个翻译单元都有自己的同名函数。

如果你会这样写

int my_func(void);
static int my_func(void);

则行为未定义,因为相同的名称被定义为具有外部和内部链接。

来自 C 标准(6.2.2 标识符的链接)

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