C++:使用“.hh”作为 C++ 头文件扩展名的原因
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10354321/
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
C++: Reason why using ".hh" as extension for C++ header files
提问by veda
I would like to know why we use ".hh" as extension for C++ header files instead of using just ".h".
我想知道为什么我们使用“.hh”作为 C++ 头文件的扩展名,而不是只使用“.h”。
The header files are preprocessed and the preprocessor doesn't even care about the extension of the header file. So, even if I create a header file with an extension ".qwe" (test.qwe). Then, why to use ".hh" as extension for C++ header files.
头文件经过预处理,预处理器甚至不关心头文件的扩展名。所以,即使我创建了一个扩展名为“.qwe”(test.qwe)的头文件。那么,为什么要使用“.hh”作为 C++ 头文件的扩展名。
Some say, we are using ".cc" as extension for C++ files to differentiate from C files (which has an extension ".c"), likewise we are using using ".hh" as extension for C++ header files to differentiate from C header files (which has an extension ".h"). I don't think this to be a valid reason.
有人说,我们使用“.cc”作为C++文件的扩展名以区别于C文件(其扩展名为“.c”),同样我们使用“.hh”作为C++头文件的扩展名以区别于C头文件(扩展名为“.h”)。我不认为这是一个正当理由。
Does anyone know the reason for naming in such a way?
有谁知道以这种方式命名的原因?
回答by Nawaz
Some say, we are using ".cc" as extension for C++ files to differentiate from C files (which has an extension ".c"), likewise we are using using ".hh" as extension for C++ header files to differentiate from C header files (which has an extension ".h").
有人说,我们使用“.cc”作为C++文件的扩展名以区别于C文件(其扩展名为“.c”),同样我们使用“.hh”作为C++头文件的扩展名以区别于C头文件(扩展名为“.h”)。
That is exactly the reason. It is just to differentiate CPP headers from C headers.
这正是原因。只是为了区分 CPP 头和 C 头。
Some programmers and libraries, such as Boost, use .hpp
for CPP headers. My personal choice is this:
一些程序员和库,如 Boost,使用.hpp
CPP 头。我个人的选择是这样的:
- example.c
- example.cpp
- example.h
- example.h++
- 例子.c
- 例子.cpp
- 例子.h
- 例子.h++
Even if they all belong to a huge project, you can still figure out which one is which. No description is needed.
即使它们都属于一个庞大的项目,您仍然可以弄清楚哪个是哪个。不需要描述。
回答by mfontanini
回答by Griwes
It's not for any specific reason, it's just a (developer- or project-specific) convention. As "file extension" is just part of name, it doesn't matter much, as source files are just plain text files. Furthermore, header files are just copy-pasted into file that included them (via #include
, so file extension mean even less for them.
这不是出于任何特定原因,它只是(特定于开发人员或项目的)约定。由于“文件扩展名”只是名称的一部分,它并不重要,因为源文件只是纯文本文件。此外,头文件只是复制粘贴到包含它们的文件中(通过#include
,因此文件扩展名对它们的意义更小。
Borland's libraries' headers had .hpp
extension. C++'s standard library headers have no extension at all. It's just a matter of convention and personal taste.
Borland 库的标题有.hpp
扩展名。C++ 的标准库头文件根本没有扩展名。这只是惯例和个人品味的问题。
回答by drew212
This is completely convention, there is no special reason for it. Usually people try to stay consistent across a project, or across teams or even an entire company. It helps developers who pick up your code after you're long gone understand that you abc.hh goes with file abc.cc and abc.h goes with abc.c.
这完全是约定俗成的,没有什么特别的理由。通常人们会尝试在整个项目、团队甚至整个公司中保持一致。它可以帮助在您离开很久之后拿起您的代码的开发人员了解您的 abc.hh 与文件 abc.cc 搭配,而 abc.h 与 abc.c 搭配。
回答by Doug T.
One reason, if I'm creating a dll that has a C API around a bunch of C++ code, I need to differentiate between my C headers which the client may use to import C functions and my internal C++ headers which I explicitly don't want to allow the client to import.
一个原因是,如果我创建的 dll 具有围绕一堆 C++ 代码的 C API,我需要区分客户端可能用于导入 C 函数的 C 头文件和我明确不使用的内部 C++ 头文件希望允许客户端导入。
Binding to a C function is pretty simple from most languages/environments with almost no overhead. You don't have to think about all the complexities inherit in C++. So if I were to work with such a library, I'd be sure to separate my C++ from my C interface.
从大多数语言/环境中绑定到 C 函数非常简单,几乎没有开销。您不必考虑 C++ 中继承的所有复杂性。因此,如果我要使用这样的库,我一定会将我的 C++ 与我的 C 接口分开。
回答by Basile Starynkevitch
I think it is mostly a social convention.
我认为这主要是一种社会惯例。
However, recent GCC compilers are able to compile a (single) header foo.hh
or foo.h
into something like foo.hh.gch
or foo.h.gch
which essentially contains a persistent memory heap image of the compiler (the cc1plus
program started by g++
) after it had parsed that header.
然而,最近的 GCC 编译器能够编译一个(单个)头文件foo.hh
或编译foo.h
成类似foo.hh.gch
或foo.h.gch
基本上包含编译器(cc1plus
程序由 启动的程序g++
)在解析该头文件后的持久内存堆映像的内容。
Notice that the C++11 standard library defines header files like e.g. <vector>
without any extension.
请注意,C++11 标准库定义了头文件,例如<vector>
没有任何扩展名。