5

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.

Melebius
  • 6,183
  • 4
  • 39
  • 52
  • Possible duplicate of [Difference between iostream and iostream.h](http://stackoverflow.com/questions/2976477/difference-between-iostream-and-iostream-h) – phuclv Nov 16 '16 at 11:28
  • I'm voting to close this question as off-topic because the standard allows for different header names and it's unlikely to be useful why any one developer chose one style over another. – paxdiablo Nov 16 '19 at 05:35

3 Answers3

13

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.

DUman
  • 2,560
  • 13
  • 16
  • 1
    actually in most modern implementations don't support `` except MSVC. Try that in other compilers and you'll get some error – phuclv Nov 16 '16 at 11:29
  • 4
    Namespaces have nothing to do with header names. – Pete Becker Nov 16 '16 at 14:12
  • @PeteBecker Certainly not directly, but wasn't the move to extension-free files done when namespaces appeared in C++98? With some implementations at the time maintaining `.h` versions that provided symbols outside namespaces, like `iostream.h` giving a global `cout` instead of `std::cout`. – DUman Nov 16 '16 at 14:22
  • 3
    Lots of things happened at about the same time, leading up to the C++98 standard. "able to drop extensions... due to the introduction of namespaces" is at best backwards. Cfront had provided iostream.h, with names in what is now known as the global namespace. When the standard library was put into `std` the issue of backward compatibility came up, and the obvious approach was to change from `iostream.h` to something else, and we decided, pretty much arbitrarily, to use names without extensions. – Pete Becker Nov 16 '16 at 14:42
  • @PeteBecker So do both #include and #include conform to the C++ standard now, or is the latter deprecated? – Kartick Vaddadi Jan 29 '18 at 01:29
  • @VaddadiKartick — `iostream.h` was never in the C++ standard. – Pete Becker Jan 29 '18 at 03:04
  • Then how were programmers supposed to use cin and cout before namespaces were added to C++? @PeteBecker – Kartick Vaddadi Jan 30 '18 at 02:58
  • 2
    @VaddadiKartick — the first C++ standard was issued in 1998. It had namespaces. It had ``. It did not have ``. – Pete Becker Jan 30 '18 at 03:03
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.

phuclv
  • 37,963
  • 15
  • 156
  • 475
0

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
Vada Poché
  • 763
  • 5
  • 16
  • wrong. In C++ to include the C standard libraries use `c`, for example ``, `` – phuclv Nov 16 '16 at 11:31
  • @LưuVĩnhPhúc That is not correct. cstdio is the C++ equivalent of C library, and it's NOT the same as C standard library. Here's another question and the top rated answer seems to be in agreement with my answer: http://stackoverflow.com/questions/7673597/include-string-or-string-h – Vada Poché Nov 16 '16 at 12:17
  • well [`stdio.h` was long deprecated](http://stackoverflow.com/a/7596439/995714) and may just get you into namespace trouble. http://stackoverflow.com/q/37904753/995714 – phuclv Nov 16 '16 at 12:43
  • @LưuVĩnhPhúc stdio.h may be deprecated, but that doesn't mean my original answer is "wrong". You're saying including the C++ equivalent headers of C library is a better thing to do, but the question was asking the difference between headers with .h extension and without it. – Vada Poché Nov 16 '16 at 21:57