extern declaration and function definition both in the same file

前端 未结 5 1806
没有蜡笔的小新
没有蜡笔的小新 2021-02-20 09:08

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

extern int main (int, char **);

int
main (int argc, cha         


        
5条回答
  •  孤街浪徒
    2021-02-20 09:49

    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 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.

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

提交回复
热议问题