为什么在C中使用目标文件? [英] Why use object files in C?

查看:85
本文介绍了为什么在C中使用目标文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我编译C程序时,为了方便起见,我最后一直在包含某个标题的源文件。因此,如果main.c包含util.h,util.h将包含util.c将使用的所有头文件,概述类型或结构等,然后在最后它包含util.c.然后,当我编译时,我只需要使用gcc main.c -o main,剩下的就全部用了。

When I compile a C program, for ease I've been including the source file for a certain header at the end. So, if main.c includes util.h, util.h will have all the headers util.c will use, outlines types or structs, etc, then at the very end it include util.c. Then, when I compile I only have to use gcc main.c -o main, and the rest is all taken care of.

我一直在查找C编码标准,试图弄清楚最好的做事方式是什么,而且有这么多,以及如此多的相互矛盾的观点,我不知道该怎么想。为什么这么多地方建议单独编译目标文件而不是将它们全部包含在网络中? util从来没有接触过util.c,所以这两者完全独立,理论上(我的理论)它会很好,但我可能错了,因为这是计算机科学,人们错了,即使他们是对的,所以,如果我已经错了,我可能错了。

I've been looking up C coding standards, trying to figure out what the best way to do things is, and there are just so many, and so many conflicting opinions I don't know what to think. Why do so many places reccomend compiling object files individually instead of including all of them in a web? util never touches anything but util.c, so the two are perfectly independent, and in theory (my theory) it would be fine, but I'm probably wrong since this is computer science and people are wrong even when they're right, so if I'm already wrong I'm probably wrong.

有人说头文件应该只是原型,而源文件是包含它的源文件,这是必要的系统标题。从纯粹作为美学的角度来看,我更喜欢在标题中使用所有信息(类型,系统标题,原型)(在本例中为util.h)并且在util.c中只包含一个函数代码(不包括一个#include) util.h在最顶层。)

Some people say header files should ONLY be prototypes, and the source file be the one that includes it, and it's necessary system headers. From purely as aesthetic point of view I much prefer having all the info (types, system headers used, prototypes) in the header (in this case util.h) and having ONLY function code in util.c (excluding one "#include "util.h"" at the very top).

我想我得到的是,所有这些东西都有效,选择一个方法听起来是随意的不了解背景的人(我)。请告诉我原因和原因。

I guess the point I'm getting at is, with all this stuff that works, selecting a method sounds arbitrary to someone who doesn't understand the background (me). Please tell me why and what.

推荐答案

虽然你的程序很小,但这样可行。但是,在某些时候,你的程序将变得足够大,每次你改变一行时重新编译整个程序是后方的痛苦。

While your program is small, this will work. At some point, however, your program will get large enough that recompiling the whole program every time you change one line is a pain in the rear.

这个 - 甚至比避免编辑大文件 - 是分割程序的原因。如果将main.c和util.c单独编译为目标文件,则在main.c中更改函数中的一行将不再需要重新编译util.c中的所有代码。

This -- even more than avoiding editing huge files -- is the reason to split up your program. If main.c and util.c are seperately compiled into object files, changing one line in a function in main.c will no longer require you to recompile all the code in util.c.

当你的程序由几十个文件组成时,这将是一个胜利。

By the time your program is made up of a few dozen files, this will be a big win.

这篇关于为什么在C中使用目标文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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