我正在阅读C项目的来源,并试图辨别为什么使用extern
的特定用途。 (注:This question是相关的,但对于C++,不C.)说我有四个头文件,每个声明一个extern const
结构:使用与定义相同的文件中的extern声明的好处?
extern const struct puppy fido;
这个结构是在源文件中定义像这样:
extern const struct puppy fido;
static const struct puppy *puppies[] = { &fido };
const struct puppy fido = { "fido", 10, 10 };
如果在源文件中将变量声明标记为extern
,那么在那些变量在同一个源文件中定义时是否有某种好处?
我认为这个帖子可能有所帮助:http://stackoverflow.com/questions/1433204/how-do-i-use-extern-to-share-variables-between-source-files-in-c –
请澄清一下,我是否正确理解这一点:你有4个带有extern声明的头文件和1个包含extern声明和定义的.c文件? – Lundin
@Lundin这是正确的。 – ybakos