c - 仅在特定功能上声明 extern 而不是在其他功能上(为了兼容性)是否有任何目的?

标签 c

据我了解,函数原型(prototype)本质上是 extern 并且向它们添加关键字不会改变功能。我在查看 Linux 内核的源代码时发现了以下内容:

extern bool console_suspend_enabled;

/* Suspend and resume console messages over PM events */
extern void suspend_console(void);
extern void resume_console(void);

int mda_console_init(void);
void prom_con_init(void);

void vcs_make_sysfs(int index);
void vcs_remove_sysfs(int index);

如您所见,有些函数有 extern 前缀,有些则没有。这似乎存在于整个项目的许多头文件中,这让我想知道:这只是一种不一致,还是出于某种(旧的)编译器兼容性原因?

来源:https://github.com/torvalds/linux/blob/master/include/linux/console.h#L188

最佳答案

is this just an inconsistency or is it for some sort of (old) compiler compatibility reason?

这肯定是编码风格的不一致。但这是无害的,因为两者是等价的。 函数声明是否存在 extern 关键字没有区别 - extern 对于函数声明是可选的。


另一方面,提供任何声明会导致微妙的错误,例如隐式函数声明(顺便说一句,隐式声明自 C99 起无效 - Are prototypes required for all functions in C89, C90 or C99?)如果函数定义在一个编译单元中用于其他编译单元。使用头文件提供跨多个编译单元的声明是常见的做法。

关于c - 仅在特定功能上声明 extern 而不是在其他功能上(为了兼容性)是否有任何目的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46201778/

相关文章:

c - 如何在C中定义可变参数

Python ConfigParser 到 C++

c - 为系统调用编写 glibc api

c - 使用 BaseCode 加密的段错误

c - sprintf(newpath, "%s%s",...) 的 sprintf_s 模拟是什么?

c++ - OpenCL 中的暴力破解(来自 CUDA 的端口)不起作用

c - 在 fopen 中使用 arg vector

编译并执行带参数的c程序

c - 按位宏有点不起作用

c - 为什么此代码不生成重新声明错误?