编译用gcc多个C文件 [英] Compiling multiple C files with gcc

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

问题描述

我有两个文件,​​ main.o中 modules.o ,我试图对它们进行编译让 main.o中可以调用函数 modules.o 。我明确告诉不要试图的#include module.o 。我真的不知道我应该不是做。我试图 GCC 的几个不同的版本(如 GCC -xc司机main.o中modules.o ),但没什么,我得到的作品:编译器不断返回

I have two files, main.o and modules.o, and I'm trying to compile them so that main.o can call functions in modules.o. I was explicitly told not to try #include module.o. I really don't know what I should be doing instead. I tried a few different versions of gcc (such as gcc -x c driver main.o modules.o), but nothing I get works: the compiler continuously returns

error: called object is not a function

的.o 文件是我的源$ C ​​$ C文件(我奉命把我的源$ C ​​$ C在文件,扩展名的.o )。我该怎么办编译此?

The .o files are my source code files (I was instructed to put my source code in files with extension .o.) What do I do to compile this?

推荐答案

您应该定义你想从modules.c调入的main.c成一个头文件中的函数,让我们说,modules.h,并包括在main.c中的头文件一旦你的头文件,请编译两个文件一起:海湾合作委员会的main.c modules.c -o输出

You should define the functions that you want to call from modules.c into main.c into a header file, let us say, modules.h, and include that header file in the main.c. Once you have the header file, please compile both of the files together: "gcc main.c modules.c -o output"

另外两个音符。首先,modules.o是一个对象文件,它不应该被包括在C源文件。第二,我们不能有一个C文件会以.o。编译生成一个.o文件时,您应该actualy得到一个错误。是这样的:

Two additional notes. First, modules.o is an object file and it should not be included in a C source file. Second, we cannot have a C file have a .o extension. You should actualy get an error when compiling a .o file. Something like:

$ cat t.o
int main() {
    int x = 1;
    return 0;
}
$
$ gcc t.o
ld: warning: in t.o, file is not of required architecture
Undefined symbols:
  "_main", referenced from:
      start in crt1.10.6.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
$

这篇关于编译用gcc多个C文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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