简体   繁体   English

独立的翻译单位

[英]Separate translation units

While asking another question I was told about the possibility of using separate translation units (files), since my current code seems to be unable to compile due to it being too large. 在询问另一个问题时,我被告知使用单独的翻译单元(文件)的可能性,因为我的当前代码似乎由于其太大而无法编译。

Now I've been looking at how to use separate translation units, but to be frank I don't even know where to get started. 现在,我一直在研究如何使用单独的翻译单元,但是坦率地说,我什至不知道从哪里开始。 If I had only a main.cpp file at the moment and I wanted to split it into for example 3 parts, where there would be one "variables1.cpp", one "variables2.cpp" and one main.cpp where it does the calculations etc, how would I do that using visual studio? 如果目前我只有一个main.cpp文件,并且想将其拆分为3个部分,则其中将有一个“ variables1.cpp”,一个“ variables2.cpp”和一个main.cpp,在其中计算等,我将如何使用Visual Studio做到这一点?

This is one of the main tasks of a programmer, how to structure the code in such a way that the logic of the code becomes clear by this "separation". 这是程序员的主要任务之一,即如何以这种“分离”使代码的逻辑变得清晰的方式来构造代码。 Some will use one class/one file. 有些将使用一个类/一个文件。

I think this question needs a very detailed answer. 我认为这个问题需要一个非常详细的答案。 Maybe you should read this article: Organizing Code Files in C and C++ 也许您应该阅读这篇文章: 用C和C ++组织代码文件

Splitting any reasonably-sized project up buys you some advantages, the most significant of which are the following: 拆分任何规模合理的项目都会给您带来一些好处,其中最重要的是:

Speed up compilation - most compilers work on a file at a time. 加快编译速度 -大多数编译器一次处理一个文件。 So if all your 10000 lines of code is in one file, and you change one line, then you have to recompile 10000 lines of code. 因此,如果所有10000行代码都在一个文件中,并且更改了一行,则必须重新编译10000行代码。 On the other hand, if your 10000 lines of code are spread evenly across 10 files, then changing one line will only require 1000 lines of code to be recompiled. 另一方面,如果将10000行代码平均分布在10个文件中,则更改一行仅需要重新编译1000行代码。 The 9000 lines in the other 9 files will not need recompiling. 其他9个文件中的9000行将不需要重新编译。 (Linking time is unaffected.) (链接时间不受影响。)

Increase organization - Splitting your code along logical lines will make it easier for you (and any other programmers on the project) to find functions, variables, struct/class declarations, and so on. 增加组织 -沿逻辑线拆分代码将使您(和项目中的任何其他程序员)更容易找到函数,变量,结构/类声明等。 Even with the ability to jump directly to a given identifier that is provided in many editors and development environments (such as Microsoft Visual C++), there will always be times when you need to scan the code manually to look for something. 即使具有直接跳转到许多编辑器和开发环境(例如Microsoft Visual C ++)中提供的给定标识符的功能,也总是需要手动扫描代码以查找某些内容。 Just as splitting the code up reduces the amount of code you need to recompile, it also reduces the amount of code you need to read in order to find something. 正如拆分代码减少了您需要重新编译的代码量一样,它也减少了为了查找内容而需要阅读的代码量。 Imagine that you need to find a fix you made to the sound code a few weeks ago. 想象一下,您需要找到几周前对声音代码所做的修复。 If you have one large file called GAME.C, that's potentially a lot of searching. 如果您有一个名为GAME.C的大文件,则可能要进行大量搜索。 If you have several small files called GRAPHICS.C, MAINLOOP.C, SOUND.C, and INPUT.C, you know where to look, cutting your browsing time by 3/4. 如果您有几个名为GRAPHICS.C,MAINLOOP.C,SOUND.C和INPUT.C的小文件,那么您知道应该在哪里查看,从而将浏览时间缩短了3/4。

Facilitate code reuse - If your code is carefully split up into sections that operate largely independently of each other, this lets you use that code in another project, saving you a lot of rewriting later. 促进代码重用 -如果将您的代码仔细地划分为可在很大程度上彼此独立的部分,则可让您在另一个项目中使用该代码,从而节省了以后的大量重写工作。 There is a lot more to writing reusable code than just using a logical file organization, but without such an organization it is very difficult to know which parts of the code work together and which do not. 编写可重用代码的不仅仅是使用逻辑文件组织,还有很多,但是如果没有这样的组织,很难知道代码的哪些部分可以协同工作,哪些不能。 Therefore putting subsystems and classes in a single file or carefully delineated set of files will help you later if you try to use that code in another project. 因此,如果您尝试在另一个项目中使用该代码,则将子系统和类放在单个文件或一组精心划定的文件中将对您有所帮助。

Share code between projects - The principle here is the same as with the reuse issue. 在项目之间共享代码 -此处的原理与重用问题相同。 By carefully separating code into certain files, you make it possible for multiple projects to use some of the same code files without duplicating them. 通过仔细地将代码分成某些文件,您可以使多个项目使用某些相同的代码文件而无需复制它们。 The benefit of sharing a code file between projects rather than just using copy-and-paste is that any bug fixes you make to that file or files from one project will affect the other project, so both projects can be sure of using the most up-to-date version. 在项目之间共享代码文件而不是仅使用复制和粘贴的好处是,您对该文件或一个项目中的文件所做的任何错误修复都会影响另一个项目,因此,两个项目都可以确保最多使用最新版本。

Split coding responsibilities among programmers - For really large projects, this is perhaps the main reason for separating code into multiple files. 在程序员之间分配编码职责 -对于大型项目,这也许是将代码分成多个文件的主要原因。 It isn't practical for more than one person to be making changes to a single file at any given time. 一个以上的人在任何给定时间对单个文件进行更改都是不切实际的。 Therefore you would need to use multiple files so that each programmer can be working on a separate part of the code without affecting the file that the other programmers are editing. 因此,您将需要使用多个文件,以便每个程序员可以在代码的单独部分上工作而不会影响其他程序员正在编辑的文件。 Of course, there still have to be checks that 2 programmers don't try altering the same file; 当然,仍然需要检查2个程序员不要尝试更改同一文件。 configuration management systems and version control systems such as CVS or MS SourceSafe help you here. 配置管理系统和版本控制系统(例如CVS或MS SourceSafe)在这里为您提供帮助。

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

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