IOW, extern是多余的,什么都不做。
这就是为什么十年后:
像Coccinelle这样的代码转换工具倾向于在函数声明中标记extern以便删除;
像git/git这样的代码库遵循这个结论,并从其代码中删除extern(适用于git 2.22, 2019年第二季度)。
参见刘丹顿(Denton- l)的commit ad6dad0, commit b199d71, commit 5545442(2019年4月29日)。
(由Junio C Hamano - gitster -在commit 4aeeef3中合并,2019年5月13日)
*.[ch]: remove extern from function declarations using spatch
There has been a push to remove extern from function declarations.
Remove some instances of "extern" for function declarations which are caught by Coccinelle.
Note that Coccinelle has some difficulty with processing functions with __attribute__ or varargs so some extern declarations are left behind to be dealt with in a future patch.
This was the Coccinelle patch used:
@@
type T;
identifier f;
@@
- extern
T f(...);
and it was run with:
$ git ls-files \*.{c,h} |
grep -v ^compat/ |
xargs spatch --sp-file contrib/coccinelle/noextern.cocci --in-place
但这并不总是那么简单:
参见刘丹顿(Denton- l)提交的7027f50(2019年9月04日)。
(由Denton Liu—Denton- l—在2019年9月5日提交7027f50中合并)
compat/*.[ch]: remove extern from function declarations using spatch
In 5545442 (*.[ch]: remove extern from function declarations using
spatch, 2019-04-29, Git v2.22.0-rc0), we removed externs from function declarations using spatch but we intentionally excluded files under compat/ since some are directly copied from an upstream and we should avoid churning them so that manually merging future updates will be simpler.
In the last commit, we determined the files which taken from an upstream
so we can exclude them and run spatch on the remainder.
This was the Coccinelle patch used:
@@
type T;
identifier f;
@@
- extern
T f(...);
and it was run with:
$ git ls-files compat/\*\*.{c,h} |
xargs spatch --sp-file contrib/coccinelle/noextern.cocci --in-place
$ git checkout -- \
compat/regex/ \
compat/inet_ntop.c \
compat/inet_pton.c \
compat/nedmalloc/ \
compat/obstack.{c,h} \
compat/poll/
Coccinelle has some trouble dealing with __attribute__ and varargs so
we ran the following to ensure that no remaining changes were left
behind:
$ git ls-files compat/\*\*.{c,h} |
xargs sed -i'' -e 's/^\(\s*\)extern \([^(]*([^*]\)/\1\2/'
$ git checkout -- \
compat/regex/ \
compat/inet_ntop.c \
compat/inet_pton.c \
compat/nedmalloc/ \
compat/obstack.{c,h} \
compat/poll/
请注意,在Git 2.24(2019年Q4)中,任何虚假的extern都被删除了。
参见Emily Shaffer (nasamuffin)的commit 65904b8(2019年9月30日)。
帮助:Jeff King (peff)。
参见刘丹顿(Denton- l)提交的8464f94(2019年9月21日)。
帮助:Jeff King (peff)。
(由Junio C Hamano—gitster—在commit 59b19bc中合并,2019年10月7日)
从函数声明中删除extern
在这个文件的创建过程中,每次引入一个新的函数声明时,都会包含一个extern。
然而,从5545442(*。[ch]:使用spatch从函数声明中删除extern, 2019-04-29, Git v2.22.0-rc0),我们一直在积极尝试防止在函数声明中使用extern,因为它们是不必要的。
移除这些虚假的外部。