简体   繁体   English

如何在C中链接两个文件

[英]How to link two files in C

I am currently working on a class assignment. 我目前正在上课。 The assignment is to create a linked list in c. 任务是在c中创建一个链表。 But because we it's a class assignment we have some constraints: 但是因为这是一个类分配,所以我们有一些限制:

We have a header file that we cannot modify. 我们有一个无法修改的头文件。 We have ac file that is the linkedlist We have ac file that is just a main method just to test the linkedlist 我们有一个ac文件,它是链表我们有一个ac文件,这只是测试链表的一种主要方法

the header file has a main method defined, so when I attempt to build the linkedlist it fails because there is no main method. 头文件定义了main方法,因此当我尝试构建链表时,它失败了,因为没有main方法。 What should I do to resolve the issue?? 我应该怎么做才能解决这个问题? Import the test file (this causes another error)? 导入测试文件(这会导致另一个错误)?

I'm assuming your three files are called header.h, main.c, and linkedlist.c 我假设您的三个文件分别称为header.h,main.c和linkedlist.c

gcc main.c linkedlist.c -o executable

This will create an executable binary called "executable" 这将创建一个称为“ executable”的可执行二进制文件。

Note this also assumes you're using gcc as a compiler. 请注意,这还假设您使用gcc作为编译器。

Like most languages, C supports modules. 像大多数语言一样,C支持模块。 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. 您将像这样编译一个模块: gcc -c linkedlist.c >这实际上将产生linkedlist.o,这是一个模块。 Try executing this linkedlist.o (after changing its mode to executable, since it won't be so by default). 尝试执行此linkedlist.o(将其模式更改为可执行后,因为默认情况下不会如此)。 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 . 您的作业似乎提供了一个测试“ main.c”,如果您想使用它,则只需将“ main.c”(实际上已编译到main.o中)链接到linkedlist.o。 To actually do that, simply type in gcc -o name_of_your_program main.c linkedlist.o . 要真正做到这一点,只需输入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. 实际上,这里要做的是,编译器首先将main.c编译为main.o模块,然后使用-o选项将两个模块以您指定的名称链接在一起,但是编译器非常聪明并且不需要明确说明他需要采取的步骤。 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. Google可以为您提供前所未有的帮助。 Good luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM