如何在C中链接两个文件?

时间:2021-12-31 23:07:02

I am currently working on a class assignment. The assignment is to create a linked list in c. But because we it's a class assignment we have some constraints:

我目前正在做一个班级作业。作业是在c中创建一个链表,但是因为我们是一个类赋值我们有一些约束条件:

We have a header file that we cannot modify. We have a c file that is the linkedlist We have a c file that is just a main method just to test the linkedlist

我们有一个无法修改的头文件。我们有一个c文件,它是linkedlist我们有一个c文件它只是用来测试linkedlist的一个主要方法。

the header file has a main method defined, so when I attempt to build the linkedlist it fails because there is no main method. What should I do to resolve the issue?? Import the test file (this causes another error)?

头文件有一个定义的main方法,所以当我尝试构建linkedlist时,它失败了,因为没有main方法。我该怎么解决这个问题?导入测试文件(这会导致另一个错误)?

2 个解决方案

#1


3  

I'm assuming your three files are called header.h, main.c, and linkedlist.c

我假设你的三个文件叫做header。h,主要。c,linkedlist.c

gcc main.c linkedlist.c -o executable

This will create an executable binary called "executable"

这将创建一个可执行的二进制文件,称为“可执行文件”

Note this also assumes you're using gcc as a compiler.

请注意,这也假定您使用gcc作为编译器。

#2


0  

Like most languages, C supports modules. What I assume your assignment requires is compiling a module. Modules, unlike full programs, lack entry points. Roughly speaking, they are collections of functions, in the manner of a library. When compiling a module, no linking is made. You would compile a module like this: gcc -c linkedlist.c -> this would actually produce linkedlist.o, which is a module. Try executing this linkedlist.o (after changing its mode to executable, since it won't be so by default). The reason you fail to execute this module is, partly, because it is not in the proper format to be executed. Ones of the reasons it is not so is it lacks entry point (what we know as 'main') and linkage. Your assignment seems to provide a test 'main.c', if you wanted to use it, you would only have to link the 'main.c' (actually compiled into main.o) with linkedlist.o . To actually do that, simply type in gcc -o name_of_your_program main.c linkedlist.o. In fact, what is being done here is that your compiler first compiles main.c into a main.o module, then links the 2 modules together under the name you have given it with the -o option, but the compiler is pretty smart and needs nothing explicit about the steps he needs to take. Now if you wanted to know more about this stuff, you'd have to try and learn about how compilers do what they do. Google can help you with that more than I ever could. Good luck.

和大多数语言一样,C支持模块。我假定您的任务需要编译一个模块。模块不像完整的程序,缺少入口点。大致说来,它们是函数的集合,以库的方式。编译模块时,不需要链接。您将编译一个这样的模块:gcc -c linkedlist。c ->实际上会产生linkedlist。o是一个模块。尝试执行这个linkedlist。o(在将其模式更改为可执行文件后,因为默认情况下不会如此)。您未能执行此模块的原因是,部分原因是它没有正确的格式执行。原因之一是它缺乏切入点(我们知道的“main”)和链接。你的作业似乎提供了一个测试的主要内容。如果你想使用它,你只需要链接“main”。c'(实际上编译成main.o)和linkedlist。o。要做到这一点,只需输入gcc -o name_of_your_program main。c linkedlist.o。实际上,这里正在做的是编译器首先编译main。c为主要。o模块,然后将两个模块连接在一起,在你给它的名字后面加上-o选项,但是编译器非常聪明,不需要明确他需要采取的步骤。现在,如果您想了解更多关于这些内容的知识,您必须尝试了解编译器是如何执行它们的工作的。谷歌比我能帮你做的更多。祝你好运。

#1


3  

I'm assuming your three files are called header.h, main.c, and linkedlist.c

我假设你的三个文件叫做header。h,主要。c,linkedlist.c

gcc main.c linkedlist.c -o executable

This will create an executable binary called "executable"

这将创建一个可执行的二进制文件,称为“可执行文件”

Note this also assumes you're using gcc as a compiler.

请注意,这也假定您使用gcc作为编译器。

#2


0  

Like most languages, C supports modules. What I assume your assignment requires is compiling a module. Modules, unlike full programs, lack entry points. Roughly speaking, they are collections of functions, in the manner of a library. When compiling a module, no linking is made. You would compile a module like this: gcc -c linkedlist.c -> this would actually produce linkedlist.o, which is a module. Try executing this linkedlist.o (after changing its mode to executable, since it won't be so by default). The reason you fail to execute this module is, partly, because it is not in the proper format to be executed. Ones of the reasons it is not so is it lacks entry point (what we know as 'main') and linkage. Your assignment seems to provide a test 'main.c', if you wanted to use it, you would only have to link the 'main.c' (actually compiled into main.o) with linkedlist.o . To actually do that, simply type in gcc -o name_of_your_program main.c linkedlist.o. In fact, what is being done here is that your compiler first compiles main.c into a main.o module, then links the 2 modules together under the name you have given it with the -o option, but the compiler is pretty smart and needs nothing explicit about the steps he needs to take. Now if you wanted to know more about this stuff, you'd have to try and learn about how compilers do what they do. Google can help you with that more than I ever could. Good luck.

和大多数语言一样,C支持模块。我假定您的任务需要编译一个模块。模块不像完整的程序,缺少入口点。大致说来,它们是函数的集合,以库的方式。编译模块时,不需要链接。您将编译一个这样的模块:gcc -c linkedlist。c ->实际上会产生linkedlist。o是一个模块。尝试执行这个linkedlist。o(在将其模式更改为可执行文件后,因为默认情况下不会如此)。您未能执行此模块的原因是,部分原因是它没有正确的格式执行。原因之一是它缺乏切入点(我们知道的“main”)和链接。你的作业似乎提供了一个测试的主要内容。如果你想使用它,你只需要链接“main”。c'(实际上编译成main.o)和linkedlist。o。要做到这一点,只需输入gcc -o name_of_your_program main。c linkedlist.o。实际上,这里正在做的是编译器首先编译main。c为主要。o模块,然后将两个模块连接在一起,在你给它的名字后面加上-o选项,但是编译器非常聪明,不需要明确他需要采取的步骤。现在,如果您想了解更多关于这些内容的知识,您必须尝试了解编译器是如何执行它们的工作的。谷歌比我能帮你做的更多。祝你好运。