问题描述
使用多个源( .cpp
)和标题( .h
)文件单项目?
What are the advantages of using multiple source (.cpp
) and header (.h
) files in a single project?
这只是一个优惠的事情还是有真正的好处?
Is it just a preferential thing or are there true benefits?
推荐答案
p>它可以帮助你拆分代码和按主题排序。否则,你会得到一个文件,其中1000行的行...这是很难管理...
It helps you split your code and sort it by theme. Otherwise you get one file with 1000s of lines… which is hard to manage…
通常,人们有.h和.c一个或有时几个类。
此外,它加速了编译,因为只有修改的文件和一些相关文件需要重新编译。
Usually, people have .h and .c for one or sometimes a few classes.Also, it speeds up compilation, since only the modified files, and some related files need to be recompiled.
从:
拆分任何规模合理的项目,都会带来一些优势,
其中最重要的是以下内容:
Splitting any reasonably-sized project up buys you some advantages, the most significant of which are the following:
-
加速编译 - 大多数编译器一次只能处理一个文件。因此,如果所有
10000行代码都在一个文件中,并且更改了一行,那么
必须重新编译10000行代码。另一方面,如果您的
10000行代码均匀分布在10个文件中,则更改
一行只需要重新编译1000行代码。
9000行在其他9个文件将不需要重新编译。 (链接
的时间不受影响。)
Speed up compilation - most compilers work on a file at a time. So if all your10000 lines of code is in one file, and you change one line, then youhave to recompile 10000 lines of code. On the other hand, if your10000 lines of code are spread evenly across 10 files, then changingone line will only require 1000 lines of code to be recompiled. The9000 lines in the other 9 files will not need recompiling. (Linkingtime is unaffected.)
增加组织 - 沿逻辑线分割代码
让你(和项目上的任何其他程序员)更容易找到
找到函数,变量,struct /类声明等等。即使
能够直接跳转到在许多编辑器和开发环境(例如
Microsoft Visual C ++)中提供的
的给定标识符,也总是有需要
手动扫描代码以查找某些内容。正如分割
代码减少了需要重新编译的代码量,它也减少了你需要读取的代码量,以便找到
。想象一下,你需要找到一个修正你对声音
代码几个星期前。如果你有一个名为GAME.C,
的大文件,这可能是很多搜索。如果你有几个小的
文件称为GRAPHICS.C,MAINLOOP.C,SOUND.C和INPUT.C,你知道
在哪里,你的浏览时间减少3/4。
Increase organization - Splitting your code along logical lines willmake it easier for you (and any other programmers on the project) tofind functions, variables, struct/class declarations, and so on. Evenwith the ability to jump directly to a given identifier that isprovided in many editors and development environments (such asMicrosoft Visual C++), there will always be times when you need toscan the code manually to look for something. Just as splitting thecode up reduces the amount of code you need to recompile, it alsoreduces the amount of code you need to read in order to findsomething. Imagine that you need to find a fix you made to the soundcode a few weeks ago. If you have one large file called GAME.C,that's potentially a lot of searching. If you have several smallfiles called GRAPHICS.C, MAINLOOP.C, SOUND.C, and INPUT.C, you knowwhere to look, cutting your browsing time by 3/4.
促进代码重用 - 如果您的代码被仔细分为
部分, b $ b你在另一个项目中使用该代码,节省了很多重写
。编写可重用代码比使用
逻辑文件组织有更多,但没有这样的组织,它是
很难知道代码的哪些部分协同工作和
不。因此,将子系统和类放在单个
文件或仔细划分的文件集中将有助于您以后如果
尝试在另一个项目中使用该代码。
Facilitate code reuse - If your code is carefully split up intosections that operate largely independently of each other, this letsyou use that code in another project, saving you a lot of rewritinglater. There is a lot more to writing reusable code than just using alogical file organization, but without such an organization it isvery difficult to know which parts of the code work together andwhich do not. Therefore putting subsystems and classes in a singlefile or carefully delineated set of files will help you later if youtry to use that code in another project.
在项目之间共享代码 - 此处的原则与
的重用问题相同。通过仔细分离代码到某些文件,你
使多个项目可以使用一些相同的代码
文件,而不复制它们。在项目之间共享代码文件
的好处是,您只需使用复制和粘贴就可以对任何
错误进行修复,一个项目的文件或其他文件将影响其他项目
Share code between projects - The principle here is the same as withthe reuse issue. By carefully separating code into certain files, youmake it possible for multiple projects to use some of the same codefiles without duplicating them. The benefit of sharing a code filebetween projects rather than just using copy-and-paste is that anybug fixes you make to that file or files from one project will affectthe other project, so both projects can be sure of using the mostup-to-date version.
在程序员中分配编码职责
Split coding responsibilities among programmers - For really largeprojects, this is perhaps the main reason for separating code intomultiple files. It isn't practical for more than one person to bemaking changes to a single file at any given time. Therefore youwould need to use multiple files so that each programmer can beworking on a separate part of the code without affecting the filethat the other programmers are editing. Of course, there still haveto be checks that 2 programmers don't try altering the same file;configuration management systems and version control systems such asCVS or MS SourceSafe help you here. All of the above can beconsidered to be aspects of modularity, a key element of bothstructured and object-oriented design.
然后,他们会继续如何做到,潜在的陷阱,修复问题等。
Then, they go on about How to do it, Potential Pitfalls, Fixing problems, etc.
您应该检查一下。
这篇关于对于项目使用多于1个代码文件的优点是什么? (C ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!