C++ header files with no extension

為{幸葍}努か 提交于 2021-02-06 14:06:08

问题


I am using an open source project (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.

I would like to know why those developer used this extension, rather than the traditional .h file extension.


回答1:


It seems you are talking about this repository of C++ code.

It looks like the authors of that code decided to follow the patterns of the C++ standard library. In standard C++, library headers are not supposed to have the .h extension. 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. 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.

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. The most common choices are to use .h or .hpp, the latter being intended to distinguish C++ headers from C headers.

A quick look at the OpenSceneGraph code shows that they've followed the C++ standard library pattern in their includes. There are no extensions, and everything is in the osg namespace, analogous to the std namespace of the standard library. So using the OpenSceneGraph libraries is very similar to using the C++ standard library.

#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. My personal opinion is that it's better to have a file extension, even if only to be able to search for header files.




回答2:


I mailed to one of the developer (Robert Osfield) of OpenSceneGraph. Here is his answer.

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.




回答3:


What I know is that most names were already taken by the C standard library. Since C++ has to co-exist with it, the C++ standard library may have evolved to not have an extension for its headers.

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


来源:https://stackoverflow.com/questions/40624930/c-header-files-with-no-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!