简体   繁体   English

在Linux中进行模块化编程和编译C程序

[英]Modular programming and compiling a C program in linux

So I have been studying this Modular programming that mainly compiles each file of the program at a time. 因此,我一直在研究这种模块化编程,它主要一次编译程序的每个文件。 Say we have FILE.c and OTHER.c that both are in the same program. 假设我们有FILE.c和OTHER.c,它们都在同一程序中。 To compile it, we do this in the prompt 要编译它,我们在提示中执行此操作

$gcc FILE.c OTHER.c -c  

Using the -c flag to compile it into .o files (FILE.o and OTHER.o) and only when that happens do we translate it (compile) to executable using 使用-c标志将其编译为.o文件(FILE.o和OTHER.o),只有在发生这种情况时,我们才使用以下命令将其翻译(编译)为可执行文件

$gcc FILE.o OTHER.o -o 

I know I can just do it and skip the middle part but as it shows everywhere, they do it first and then they compile it into executable, which I can't understand at all. 我知道我可以做到这一点,而跳过中间部分,但是正如它在各处所显示的那样,他们先这样做,然后将其编译为可执行文件,我根本无法理解。

May I know why? 我可以知道为什么吗?

If you are working on a project with several modules, you don't want to recompile all modules if only some of them have been modified. 如果您正在处理具有多个模块的项目,则只修改了其中一些模块,就不想重新编译所有模块。 The final linking command is however always needed. 但是,始终需要最终的链接命令。 Build tools such as make is used to keep track of which modules need to be compiled or recompiled. 使用诸如make之类的构建工具来跟踪需要编译或重新编译哪些模块。

Doing it in two steps allows to separate more clearly the compiling and linking phases. 分两个步骤进行操作可以更清楚地分离编译和链接阶段。

  • The output of the compiling step is object (.o) files that are machine code but missing the external references of each module (ie each c file); 编译步骤的输出是目标(.o)文件,这些文件是机器代码,但是缺少每个模块的外部引用(即每个c文件); for instance file.c might use a function defined in other.c, but the compiler doesn't care about that dependency in that step; 例如file.c可能使用other.c中定义的函数,但是编译器在该步骤中并不关心该依赖关系。

  • The input of the linking step is the object files, and its output is the executable. 链接步骤的输入是目标文件,其输出是可执行文件。 The linking step bind together the object files by filling the blanks (ie resolving dependencies between objets files). 链接步骤通过填充空白(即解决objets文件之间的依赖关系)将目标文件绑定在一起。 That's also where you add the libraries to your executable. 这也是将库添加到可执行文件的地方。

This part of another answer responds to your question: 另一个答案的这一部分将回答您的问题:

You might ask why there are separate compilation and linking steps. 您可能会问,为什么有单独的编译和链接步骤。 First, it's probably easier to implement things that way. 首先,以这种方式实现事情可能更容易。 The compiler does its thing, and the linker does its thing -- by keeping the functions separate, the complexity of the program is reduced. 编译器完成其任务,链接器完成其任务-通过将功能分开,可以降低程序的复杂性。 Another (more obvious) advantage is that this allows the creation of large programs without having to redo the compilation step every time a file is changed. 另一个(更明显的)优点是,这允许创建大型程序,而不必在每次更改文件时都重做编译步骤。 Instead, using so called "conditional compilation", it is necessary to compile only those source files that have changed; 而是使用所谓的“条件编译”,仅编译那些已更改的源文件; for the rest, the object files are sufficient input for the linker. 对于其余部分,目标文件对于链接器来说是足够的输入。 Finally, this makes it simple to implement libraries of pre-compiled code: just create object files and link them just like any other object file. 最后,这使实现预编译代码库变得简单:只需创建目标文件并像其他任何目标文件一样链接它们即可。 (The fact that each file is compiled separately from information contained in other files, incidentally, is called the "separate compilation model".) (顺便说一句,每个文件都与其他文件中包含的信息分开编译的事实被称为“单独的编译模型”。)

It was too long to put in a comment, please give credit to the original answer. 评论太久了,请相信原始答案。

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

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