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

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

问题描述

我有以下两个文件:

file1.c

int main(){
  foo();
  return 0;
}

file2.c

void foo(){

 }

我可以将两个文件编译并链接在一起,以便 file1.c 在不添加 extern 的情况下识别 foo 函数吗?

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 的隐式声明.

gcc file1.c file2.c throws: warning: implicit declaration of function 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文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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