home Glamenv-Septzen(ぐらめぬ・ぜぷつぇん)(archive)

日記/2010/07/13/ヘッダーファイルの関数宣言にexternって必要?

作成日: 2010-07-13 23:04:15   /   last updated at: 2010-07-13 23:13:03
カテゴリ: C言語 

cheap2el作ってる時、少し気になってたのでGoogle先生に尋ねてみたら、関数についてはexternをわざわざつける必要は無かったみたいです。

一応ソースの首根っこをメモ。上記Wikipediaから辿れる、"The current Standard (C99 with Technical corrigenda TC1, TC2, and TC3 included)", WG14/N1256 Committee Draft(Septermber 7, 2007) ISO/IEC 9899:TC3 より:

6.2.2 Linkages of identifiers
...
[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.

昔C言語勉強してた時に、libを作る時にincludeするときはextern無しで、外部からリンクする為にincludeするときはextern有りにするとかでわざわざ

#ifdef __BUILD_LIB__
#define Extern extern
#endif
...
Extern int foobar(int a, int b);

とかして、libを作る時と、アプリ側でリンクする時とで define に応じて切り替えてるサンプルを見たことがあったので気になってましたが・・・無意味だったんだなー・・・。

original url: https://www.glamenv-septzen.net/view/704