50

I'm playing around with gmock and noticed it contains this line:

#include <tuple>

I would have expected tuple.h.

When is it okay to exclude the extension, and does it give the directive a different meaning?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Gordon Wilson
  • 26,244
  • 11
  • 57
  • 60

7 Answers7

69

The C++ standard headers do not have a ".h" suffix. I believe the reason is that there were many, different pre-standard implementations that the standard would break. So instead of requiring that vendors change their exiting "iostream.h" (for example) header to be standards compliant (which would break their existing user's code), the standards committee decided that they'd drop the suffix (which, I believe no then existing implementation had already done).

That way, existing, non-standard programs would continue to work using the vendor's non-standard libraries. When the user wanted to make their programs standards compliant, one of the steps they would take is to change the "#include" directive to drop the ".h" suffix.

So

#include <iostream>     // include the standard library version
#include <iostream.h>   // include a vendor specific version (which by 
                        //      now might well be the same)

As other answers have mentioned, writers of non-standard libraries may choose either naming convention, but I'd think they would want to continue using ".h" or ".hpp" (as Boost has done) for a couple reasons:

  1. if & when the library gets standardized, the standard version won't automatically override the previous, non-standard one (causing broken user code in all likelihood)
  2. it seems to be a convention (more or less) that headers without a suffix are standard libraries, and those with a suffix (other than the old C headers) are non-standard.

Note that a similar problem happened when the committee went to add hash maps to the STL - they found that there are already many (different) hash_map implementations that exist, so instead of coming up with a standard one that breaks a lot of stuff out there today, they are calling the standard implementation "unordered_map". Namespaces were supposed to help prevent this type of jumping through hoops, but it didn't seem to work well enough (or be used well enough) to allow them to use the more natural name without breaking a lot of code.

Note that for the 'C' headers, C++ allows you to include either a <cxxxxxx> or <xxxxxx.h> variant. The one that starts with 'c' and has no ".h" suffix put their declarations in the std namespace (and possibly the global namespace), the ones with the ".h" suffix put the names in the global namespace (some compilers also put the names in the std namespace - it's unclear to me if that's standard compliant, though I don't see the harm).

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
Michael Burr
  • 333,147
  • 50
  • 533
  • 760
  • 8
    another (tho probably not the main reason :)) might be to point at the fact standard headers aren't required to be files at all. so they could have decided to drop the ".h", because it suggests a file-extension. – Johannes Schaub - litb Jan 14 '09 at 04:26
  • 5
    This has been true since ANSI C 1989, which has as a footnote: "A header is not necessarily a source file..." By the way - is anyone aware of a compiler that does something other than normal source files for standard headers? – Michael Burr Jan 14 '09 at 05:42
  • 3
    @MichaelBurr Wouldn't precompiled headers fit that bill? – Peter - Reinstate Monica Oct 13 '15 at 14:04
  • Proper use of namespaces indeed would have resolved any potential conflicts between user code and standard library code, unfortunately many consumers of the STL library have some form of `using namespace std;` lingering in their source files, and unfortunately (since in C++, backwards compatibility is king) the committee decided this warranted the use of a different name. – Tyg13 Mar 27 '19 at 14:13
20

If the file is named tuple then you need to #include <tuple> if it's named tuple.h then you need to #include <tuple.h>

It's as simple as that. You are not omitting any extension.

Ferruccio
  • 98,941
  • 38
  • 226
  • 299
15

It's including a file that is simply named "tuple" -- the file itself lacks an extension.

The putative standard for C++ include files is to name them without the .h extension; many library writers follow this standard (STL,etc) but some do not.

Crashworks
  • 40,496
  • 12
  • 101
  • 170
8

There is nothing special going on. The file is simply named tuple.

The reason for this...that standard library headers have no file extention are because of namespaces.

Namespaces were added to the C++ standard late in the game with the C++98 standard, including the std namespace that all standard library entities reside in.

When the standard library got moved to the std namespace, that meant that all existing C++ code would break since all of it expected the library to be in the global namespace. The solution was to leave the old "dot-h" header files alone and provide the namespaced library in files that have no extension.

This way, old code that would #include<iosteam.h> could expect a global cout while new code could #include<iostream> and expect a std::cout.

Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
  • Just found an statement to the opposite and wanted to point to it. Peter Becker states that ["Namespaces have nothing to do with header names."](https://stackoverflow.com/questions/40624930/c-header-files-with-no-extension#comment68500878_40628360) – Daniel K. Jan 03 '21 at 10:16
  • @DanielK. I believe that statement is taken out of context. Did you read Peter Becker's very next comment? It seems to retell the explanation in this answer. – Drew Dormann Jan 03 '21 at 17:22
  • 1
    I think you are right. [Peter Becker's next comment](https://stackoverflow.com/questions/40624930/c-header-files-with-no-extension#comment68502179_40628360) states "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." – Daniel K. Aug 10 '21 at 20:12
5

In additional to the fine answers already posted, it should be noted that the C++ standard does not require the directive "#include <iostream>" to read a file named "iostream", or even "iostream.h". It could be named "fuzzball". Or, no corresponding file may exist and the definitions be built into the compiler and activated by the include directive.

Darron
  • 21,309
  • 5
  • 49
  • 53
4

My understanding was that #include tuple would "point" to tuple.h.

Check this out: iostream vs iostream.h

nmiller
  • 293
  • 2
  • 3
  • 10
  • 1
    The compiler might decide it does. The conversion from the string in source code to complete file path for the OS is up to the compiler. Directory prefixing is quite common, but suffixing extensions isn't – MSalters Jan 14 '09 at 10:20
  • The link is broken – Enbugger Dec 14 '18 at 15:58
-3

Folks,

I think the deal is: #include <lib> allways prepends /lib/include to the search path (the .h is infrerred) whereas #include <lib.h> searches just -I<pathname>.

Please note that I could be wrong... It's just how I think it works (in Forte cc on Solaris).

corlettk
  • 13,288
  • 7
  • 38
  • 52