简体   繁体   English

C++ header 文件无扩展名

[英]C++ header files with no extension

I am using an open source project (Open Scene Graph).我正在使用一个开源项目(Open Scene Graph)。 I found that all the header file names are in File format, which I found to be File With No Extension as mentioned in some website.我发现所有 header 文件名都是File格式,我发现它是某些网站中提到的无扩展名的文件。

I would like to know why those developer used this extension, rather than the traditional .h file extension.我想知道为什么那些开发人员使用这个扩展名,而不是传统的.h文件扩展名。

It seems you are talking about this repository of C++ code. 看来您在谈论的是C ++代码存储库

It looks like the authors of that code decided to follow the patterns of the C++ standard library. 该代码的作者似乎决定遵循C ++标准库的模式。 In standard C++, library headers are not supposed to have the .h extension. 在标准C ++中,库标头不应具有.h扩展名。 So the following is correct: 所以以下是正确的:

#include <iostream> 

With most implementations writing <iostream.h> would also work, but the version without an extension is actually correct. 在大多数实现中,编写<iostream.h>也可以,但是没有扩展名的版本实际上是正确的。 The C++ standard library was able to drop extensions in C++98 due to the introduction of namespaces, and introduction of the std namespace for the standard library. 由于引入了命名空间以及标准库的std命名空间,C ++标准库能够删除C ++ 98中的扩展。

The C++ standard neither requires nor forbids an extension for other headers, so it's entirely up to the authors of some software what file extension to use, if any. C ++标准既不需要也不禁止扩展其他标头,因此,完全由某些软件的作者决定使用什么文件扩展名(如果有)。 The most common choices are to use .h or .hpp , the latter being intended to distinguish C++ headers from C headers. 最常见的选择是使用.h.hpp ,后者旨在区分C ++头文件和C头文件。

A quick look at the OpenSceneGraph code shows that they've followed the C++ standard library pattern in their includes. 快速浏览OpenSceneGraph代码表明它们遵循了包含中的C ++标准库模式。 There are no extensions, and everything is in the osg namespace, analogous to the std namespace of the standard library. 没有扩展,一切都在osg命名空间中,类似于标准库的std命名空间。 So using the OpenSceneGraph libraries is very similar to using the C++ standard library. 因此,使用OpenSceneGraph库与使用C ++标准库非常相似。

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

It's the same pattern as: 与以下模式相同:

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

So I think it's safe to say that authors of the OSG wanted to follow the same pattern as in the C++ Standard Library. 因此,我认为可以肯定地说OSG的作者希望遵循与C ++标准库中相同的模式。 My personal opinion is that it's better to have a file extension, even if only to be able to search for header files. 我个人的观点是,最好具有文件扩展名,即使仅能够搜索头文件也是如此。

I mailed to one of the developer (Robert Osfield) of OpenSceneGraph. 我邮寄了OpenSceneGraph的一名开发人员(罗伯特·奥斯菲尔德)。 Here is his answer. 这是他的答案。

The OSG adopted the same header convention as the standard C++ headers. OSG采用与标准C ++头相同的头约定。 We have added a - -C++- - string to the headers so that editors can use this to determine the type. 我们在标题中添加了--C ++--字符串,以便编辑者可以使用它来确定类型。

What I know is that most names were already taken by the C standard library. 我所知道的是,大多数名称已经被C标准库采用。 Since C++ has to co-exist with it, the C++ standard library may have evolved to not have an extension for its headers. 由于C ++必须与之共存,因此C ++标准库可能已经演变为没有其标头扩展。

Note that these some of these headers may have the same name, but may or may not be similar in the functionality they offer. 请注意,这些标头中的某些标头可能具有相同的名称,但它们提供的功能可能相同,也可能不同。

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

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

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