C++ 为什么将标题放在单独的目录中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13967296/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:51:44  来源:igfitidea点击:

Why place headers in a separate directory?

c++cdirectory-structure

提问by Doug Moore

I know that it is common in C/C++ projects to place header files in a directory such as includeand implementation in a separate directory such as src. I have been toying with different project structures and am wondering whether there any objective reasons for this or is it simply convention?

我知道在 C/C++ 项目中将头文件放在一个目录中是很常见的,比如将include实现放在一个单独的目录中,比如src. 我一直在玩弄不同的项目结构,想知道这是否有任何客观原因,还是仅仅是约定俗成?

回答by Luchian Grigore

Convention is one of the reasons - most of the time, with effective abstraction, you only care about the interface and want to have it easy just looking at the headers.

约定是原因之一 - 大多数情况下,通过有效的抽象,您只关心界面并希望仅查看标题就可以轻松完成。

It's not the only reason though. If your project is organised in modules, you most likely have to include some headers in different modules, and you want your include directory to be cleaned of other "noise" files in there.

但这不是唯一的原因。如果你的项目是按模块组织的,你很可能必须在不同的模块中包含一些头文件,并且你希望你的包含目录中的其他“噪音”文件被清除。

Also, if you plan on redistributing your module, you probably want to hide implementation details. So you only supply headers and binaries - and distributing headers from a single folder with nothing else in it is simpler.

此外,如果您计划重新分发您的模块,您可能希望隐藏实现细节。所以你只提供头文件和二进制文件 - 从一个文件夹中分发头文件而没有其他东西更简单。

There's also an alternativewhich I actually prefer - public headers go in a separate folder (these contain the minimum interface - no implementation details are visible whatsoever), and private headers and implementation files are separate (possibly, but not necessarily, in separate folders).

还有一个我更喜欢的替代方案- 公共头文件放在一个单独的文件夹中(这些包含最小的接口 - 没有任何实现细节可见),并且私有头文件和实现文件是分开的(可能,但不一定,在单独的文件夹中) .

回答by user877329

I prefer putting them into the samedirectory. Reason:

我更喜欢把它们放在同一个目录中。原因:

The interface specification file(s), and the source file(s) implementing that interface belongs to the same part of the project. Say you have subsystemx. Then, if you put subsystemxfiles in the subsystemxdirectory, subsustemxis self-contained.

接口规范文件和实现该接口的源文件属于项目的同一部分。说你有subsystemx。然后,如果你把subsystemx文件放在subsystemx目录中,subsustemx是自包含的。

If there are many include files, sure you could do subsystemx/includeand subsystemx/source, but then I argue that if you put the definition of class Fooin foo.hpp, and foo.cppyou certainly want to see both of them (or at least have the possibility to do so easily) together in a directory listing. Finding all files related to foo

如果有很多包含文件,当然你可以做subsystemx/includeand subsystemx/source,但是我认为如果你把class Fooin的定义放在一起foo.hppfoo.cpp你当然希望看到它们(或者至少有可能很容易地这样做)一起在一个目录列表。查找所有相关文件foo

ls foo*

Finding all implementation files:

查找所有实现文件:

ls *.cpp

Finding all declaration files:

查找所有声明文件:

ls *.hpp

Simple and clean.

简单干净。

回答by Robert Allan Hennigan Leahy

It keeps your folder structure cleaner. Headers and source files are distinctly different, and are used for different things, so it makes sense to separate them. From this point-of-view the question is basically the same as "why do source files and documentation go in different folders"? The computer is highly agnostic about what you put in folders and what you don't, folders are -- for the most part -- just a handy abstraction because of the way that we humans parse, store, and recall information.

它使您的文件夹结构更清晰。头文件和源文件明显不同,用于不同的事情,因此将它们分开是有意义的。从这个角度来看,问题与“为什么源文件和文档放在不同的文件夹中”基本相同?计算机对您放入文件夹中的内容和不放入的内容高度不可知,文件夹——在大多数情况下——只是一种方便的抽象,因为我们人类解析、存储和回忆信息的方式。

There's also the fact that header files remain useful even after you've built, i.e. if you're building a library and someone wants to use that library, they'll need the header files -- not the source files -- so it makes bundling those header files up -- grabbing the stuff in binand the stuff in includeand not having to sift through src-- much easier.

还有一个事实是,即使在您构建之后,头文件仍然有用,即如果您正在构建一个库并且有人想要使用该库,他们将需要头文件——而不是源文件——所以它使得将这些头文件捆绑在一起——获取内容bin和内容include而不必筛选src——更容易。

回答by SF.

Besides (arguable?) usefulness for keeping things orderly, useful in other projects etc, there is one very neutral and objective advantage: compile time.

除了(有争议的?)保持秩序的有用性,在其他项目中有用等,还有一个非常中立和客观的优势:编译时间。

In particular, in a big project with a whole bunch of files, depending on search paths for the headers (.c/.cpp files using #include "headername.h"rather than #include "../../gfx/misc/something/headername.h"and the compiler passed the right parameters to be able to swallow that) you drastically reduce the number of entries that need to be scanned by the compiler in search of the right header. Since most compilers start separately for each file compiled, they need to read in the list of files on the include path and seek the right headers for each compiled file. If there is a bunch of .c, .o and other irrelevant files on the include path, finding the includes among them takes proportionally longer.

特别是,在一个包含一大堆文件的大项目中,根据头文件的搜索路径(.c/.cpp 文件使用#include "headername.h"而不是#include "../../gfx/misc/something/headername.h",编译器传递了正确的参数以能够吞下它)你会大大减少数量需要由编译器扫描以搜索正确标头的条目。由于大多数编译器为每个编译的文件分别启动,它们需要读取包含路径上的文件列表并为每个编译的文件寻找正确的头文件。如果包含路径上有一堆 .c、.o 和其他不相关的文件,则在其中查找包含的时间成比例地更长。

回答by parasrish

In short, a few reasons:

简而言之,有几个原因:

  • Maintainable code.
  • Code is well-designed and neat.
  • Faster compile time (at times, for minor changes done).
  • Easier segregation of the Interfaces for documentation etc.
  • Cyclic dependency at compile time can be avoided.
  • Easy to review.
  • 可维护的代码。
  • 代码设计良好且整洁。
  • 更快的编译时间(有时,对于完成的小改动)。
  • 更容易隔离文档等的接口。
  • 可以避免编译时的循环依赖。
  • 易于。

Have a look at the article Organizing Code Files in C and C++ which explains it well.

看看在 C 和 C++ 中组织代码文件的文章,它很好地解释了它。