编译程序中的多个C文件 [英] Compiling multiple C files in a program
问题描述
我有以下两个文件:
file1.c中
INT的main(){
富();
返回0;
}
file2.c中
无效美孚(){ }
我可以编译和两个文件链接在一起,让 file1.c中
将识别富
功能,无需添加的extern
?
更新的雏形。
GCC在file1.c file2.c中抛出:警告:函数foo的隐式声明
块引用>解决方案您不需要
的extern
,但在file1.c必须看到,<$ C的声明$ C>富()存在。通常这个声明是在头文件。要添加一个向前声明不使用头文件,只需修改file1.c中为:
INT富(); //添加此声明诠释主(){
富();
返回0;
}I have the following two files:
file1.c
int main(){ foo(); return 0; }
file2.c
void foo(){ }
Can I compile and link the two files together so the
file1.c
will recognize thefoo
function without addingextern
?Updated the prototype.
gcc file1.c file2.c throws: warning: implicit declaration of function foo.
解决方案You don't need an
extern
, but file1.c must see a declaration thatfoo()
exists. Usually this declaration is in a header file.To add a forward declaration without using a header file, simply modify file1.c to:
int foo(); // add this declaration int main(){ foo(); return 0; }
这篇关于编译程序中的多个C文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!