我的类定义一直使用*.h文件,但在阅读了一些boost库代码后,我意识到它们都使用*.hpp。我一直很讨厌这个文件扩展名,我想主要是因为我不习惯它。
使用*.hpp而不是*.h的优点和缺点是什么?
我的类定义一直使用*.h文件,但在阅读了一些boost库代码后,我意识到它们都使用*.hpp。我一直很讨厌这个文件扩展名,我想主要是因为我不习惯它。
使用*.hpp而不是*.h的优点和缺点是什么?
当前回答
Bjarne Stroustrup和Herb Sutter在他们的c++核心指南(https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#S-source)中对这个问题有一个声明,该指南也提到了标准扩展(c++ 11, c++ 14等)的最新变化。
SF.1: Use a .cpp suffix for code files and .h for interface files if your Y project doesn't already follow another convention Reason It's a longstanding convention. But consistency is more important, so if your project uses something else, follow that. Note This convention reflects a common use pattern: Headers are more often shared with C to compile as both C++ and C, which typically uses .h, and it's easier to name all headers .h instead of having different extensions for just those headers that are intended to be shared with C. On the other hand, implementation files are rarely shared with C and so should typically be distinguished from .c files, so it's normally best to name all C++ implementation files something else (such as .cpp). The specific names .h and .cpp are not required (just recommended as a default) and other names are in widespread use. Examples are .hh, .C, and .cxx. Use such names equivalently. In this document, we refer to .h and .cpp > as a shorthand for header and implementation files, even though the actual extension may be different. Your IDE (if you use one) may have strong opinions about suffices.
我不是这种惯例的大粉丝,因为如果你正在使用像boost这样的流行库,你的一致性已经被打破了,你最好使用.hpp。
其他回答
我使用.hpp是因为我想让用户区分哪些头文件是c++头文件,哪些头文件是C头文件。
当你的项目同时使用C和c++模块时,这一点非常重要:就像其他人在我之前解释的那样,你应该非常小心地做这件事,它从你通过扩展提供的“契约”开始
.hpp: c++头文件
(或。hxx,或。hh,或其他)
此头文件仅适用于c++。
如果你在C模块,甚至不要尝试包含它。你不会喜欢它,因为没有努力使它对c友好(太多的东西会丢失,比如函数重载、名称空间等等)。
.h:兼容C/ c++或纯C头文件
C源代码和c++源代码都可以直接或间接地包含这个头文件。
它可以直接包含,由__cplusplus宏保护:
这意味着,从c++的观点来看,与C兼容的代码将被定义为extern“C”。 从C的角度来看,所有的C代码都是清晰可见的,但c++代码是隐藏的(因为它不会在C编译器中编译)。
例如:
#ifndef MY_HEADER_H
#define MY_HEADER_H
#ifdef __cplusplus
extern "C"
{
#endif
void myCFunction() ;
#ifdef __cplusplus
} // extern "C"
#endif
#endif // MY_HEADER_H
或者它可以通过相应的.hpp头以extern“C”声明间接包含。
例如:
#ifndef MY_HEADER_HPP
#define MY_HEADER_HPP
extern "C"
{
#include "my_header.h"
}
#endif // MY_HEADER_HPP
and:
#ifndef MY_HEADER_H
#define MY_HEADER_H
void myCFunction() ;
#endif // MY_HEADER_H
下面是C和c++头文件命名不同的几个原因:
Automatic code formatting, you might have different guidelines for formatting C and C++ code. If the headers are separated by extension you can set your editor to apply the appropriate formatting automatically Naming, I've been on projects where there were libraries written in C and then wrappers had been implemented in C++. Since the headers usually had similar names, i.e. Feature.h vs Feature.hpp, they were easy to tell apart. Inclusion, maybe your project has more appropriate versions available written in C++ but you are using the C version (see above point). If headers are named after the language they are implemented in you can easily spot all the C-headers and check for C++ versions.
记住,C不是c++,除非你知道你在做什么,否则混合和匹配是非常危险的。适当地命名源代码可以帮助您区分语言。
源文件的扩展名可能对您的构建系统有意义,例如,您可能在makefile中有一个用于.cpp或. C文件的规则,或者您的编译器(例如Microsoft cl.exe)可能根据扩展名将文件编译为C或c++。
因为您必须向#include指令提供整个文件名,所以头文件扩展名是不相关的。如果您愿意,您可以在另一个源文件中包含.c文件,因为它只是一个文本包含。你的编译器可能有一个选项来转储预处理输出,这将使这一点明确(微软:/P预处理到文件,/E预处理到stdout, /EP省略#line指令,/C保留注释)
你可以选择对那些只与c++环境相关的文件使用.hpp,也就是说,它们使用的特性不能用C编译。
工具和人类很容易区分事物。就是这样。
在常规使用中(通过boost等),.hpp是特别的c++头文件。另一方面,.h用于非c++专用的头文件(主要是C)。由于存在许多非平凡的情况,因此精确检测内容的语言通常很难,因此这种差异通常使现成的工具易于编写。对于人类来说,一旦习惯了,也就容易记忆和使用了。
然而,我要指出,公约本身并不总是如预期的那样有效。
它不受语言规范的强迫,无论是C还是c++。有许多项目不遵循惯例。一旦你需要合并(混合)它们,就会很麻烦。 .hpp本身不是唯一的选择。为什么不是。hh或。hxx?(尽管无论如何,您通常至少需要一个关于文件名和路径的常规规则。)
我个人在我的c++项目中使用。h和。hpp。我没有遵循上面的惯例,因为:
The languages used by each part of the projects are explicitly documented. No chance to mix C and C++ in same module (directory). Every 3rdparty library is required to conforming to this rule. The conformed language specifications and allowed language dialects used by the projects are also documented. (In fact, I even document the source of the standard features and bug fix (on the language standard) being used.) This is somewhat more important than distinguishing the used languages since it is too error-prone and the cost of test (e.g. compiler compatibility) may be significant (complicated and time-consuming), especially in a project which is already in almost pure C++. Filenames are too weak to handle this. Even for the same C++ dialect, there may be more important properties suitable to the difference. For example, see the convention below. Filenames are essentially pieces of fragile metadata. The violation of convention is not so easy to detect. To be stable dealing the content, a tool should eventually not only depend on names. The difference between extensions is only a hint. Tools using it should also not be expected behave same all the time, e.g. language-detecting of .h files on github.com. (There may be something in comments like shebang for these source files to be better metadata, but it is even not conventional like filenames, so also not reliable in general.)
我通常在c++的头文件中使用.hpp,并且头文件应该只以头文件的方式使用(维护),例如作为模板库。对于.h中的其他头文件,要么有相应的.cpp文件作为实现,要么是非c++头文件。后者对于人工(或具有显式嵌入元数据的工具,如果需要的话)通过头的内容进行区分是微不足道的。
我最近开始在c++头文件中使用*.hpp。
原因是我使用emacs作为我的主编辑器,当你加载一个*.h文件时,它会自动进入c模式,当你加载一个*.hpp文件时,它会自动进入c++模式。
除此之外,我没有看到选择*.h而不是*.hpp或相反的理由。