没有扩展名的 C++ 头文件

C++ header files with no extension

我正在使用一个开源项目 (Open Scene Graph)。我发现所有头文件名都是File格式,我发现是某些网站上提到的没有扩展名的文件

我想知道为什么那些开发人员使用这个扩展名,而不是传统的 .h 文件扩展名。

据我所知,大多数名称已被 C 标准库采用。由于 C++ 必须使用它 co-exist,C++ 标准库可能已经发展到没有对其 headers.

的扩展

请注意,其中一些 headers 可能具有相同的名称,但它们提供的功能可能相似也可能不同。

#include<some.h> //this includes the header C library
#include<some> //this includes the header from the C++ standard library

您似乎在谈论 this repository 的 C++ 代码。

看起来该代码的作者决定遵循 C++ 标准库的模式。在标准 C++ 中,库 header 不应具有 .h 扩展名。所以以下是正确的:

#include <iostream> 

大多数实现写 <iostream.h> 也可以,但没有扩展名的版本实际上是正确的。由于命名空间的引入以及标准库的 std 命名空间的引入,C++ 标准库能够在 C++98 中删除扩展。

C++ 标准既不要求也不禁止其他 header 的扩展名,因此使用什么文件扩展名(如果有)完全取决于某些软件的作者。最常见的选择是使用 .h.hpp,后者旨在区分 C++ headers 和 C headers.

快速浏览一下 OpenSceneGraph 代码就会发现,他们在包含的内容中遵循了 C++ 标准库模式。没有扩展,所有内容都在 osg 命名空间中,类似于标准库的 std 命名空间。所以使用 OpenSceneGraph 库与使用 C++ 标准库非常相似。

#include <osg/Camera> // Provides osg::Camera

与以下模式相同:

#include <string> //Provides std::string

所以我认为可以肯定地说,OSG 的作者希望遵循与 C++ 标准库中相同的模式。我个人的意见是最好有一个文件扩展名,即使只能搜索 header 个文件。

我给 OpenSceneGraph 的一位开发人员 (Robert Osfield) 发了邮件。这是他的回答。

The OSG adopted the same header convention as the standard C++ headers. We have added a --C++-- string to the headers so that editors can use this to determine the type.