本文介绍了在一个程序中编译多个 C 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下两个文件:
file1.c
int main(){
foo();
return 0;
}
file2.c
void foo(){
}
我可以编译两个文件并将其链接在一起,以便 file1.c
将识别 foo
函数而不添加 extern
?
Can I compile and link the two files together so the file1.c
will recognize the foo
function without adding extern
?
更新了原型.
gcc file1.c file2.c 抛出:警告:函数 foo 的隐式声明.
推荐答案
你不需要extern
,但是file1.c必须看到foo()
存在.通常这个声明在头文件中.
You don't need an extern
, but file1.c must see a declaration that foo()
exists. Usually this declaration is in a header file.
要在不使用头文件的情况下添加前向声明,只需将 file1.c 修改为:
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 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!