编译程序中的多个C文件 [英] Compiling multiple C files in a program

查看:132
本文介绍了编译程序中的多个C文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下两个文件:

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 the foo function without adding extern?

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 that foo() 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屋!

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