无论是在同一个文件外部声明和函数定义的 [英] extern declaration and function definition both in the same file

查看:246
本文介绍了无论是在同一个文件外部声明和函数定义的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是通过 GCC 源文件浏览。在 gcc.c ,我发现类似

I was just browsing through gcc source files. In gcc.c, I found something like

extern int main (int, char **);

int
main (int argc, char **argv)
{

现在我的疑问是的extern 是告诉编译器的特定功能不在此文件,但会在项目的其他地方找到。但在这里,的定义主要的extern后,立即声明。什么样的目的是的extern 声明再服?

Now my doubt is extern is to tell the compiler that the particular function is not in this file but will be found somewhere else in the project. But here, definition of main is immediately after the extern declaration. What purpose is the extern declaration serving then?

这似乎是,在这个具体的例子,的extern 似乎表现得像导出,我们在组装使用,wherin我们出口的模块之外的特定符号

It seems like, in this specific example, extern seems to be behaving like export that we use in assembly, wherin we export a particular symbol outside of the module

任何想法?

推荐答案

您误解了的extern - 它的的告诉编译器定义在的其他的文件,它只是宣告它的存在而不定义它。这是完全没关系它在被定义的相同的文件。

You are misunderstanding the extern - it does not tell the compiler the definition is in another file, it simply declares that it exists without defining it. It's perfectly okay for it to be defined in the same file.

C的声明(声​​明的东西存在,而无需将其定义)和定义(实际上把它纳入存在)的概念。你可以的声明的东西往往你想,但一次只能定义它。

C has the concept of declaration (declaring that something exists without defining it) and definition (actually bringing it into existence). You can declare something as often as you want but can only define it once.

由于功能在默认情况下外部联动,的extern 关键字是无关紧要在这种情况下。

Because functions have external linkage by default, the extern keyword is irrelevant in this case.

这篇关于无论是在同一个文件外部声明和函数定义的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆