Jump to content
  • Advertisement
Sign in to follow this  
mike74

.h or not

This topic is 4814 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This is sort of a trivial question, but it's been perplexing me. What determines whether you need to put a .h at the end of an include in C++? For instance: #include <cstdlib> #include <iostream> #include <hash_map.h> #include <conio.h> Why is it that #include <hash_map> doesn't work? Or #include <conio>? I would think both of these headers are fairly standard. Mike C. http://www.coolgroups.com/zoomer/

Share this post


Link to post
Share on other sites
Advertisement
The headers without the .h are the newer version. For example, there are two iostream files, one with the .h and one without it. The one with the extension is old and deprecated, the one without it belongs to the newer standard.

Share this post


Link to post
Share on other sites
The Standard C++ Libarary (formerly STL) headers are all specified without the extension. Existing standard C headers all have the extension. For anything else, it would depend on the provider of the libraries/headers. It is still common practice to include the extension, but not required. Note that it is also common to see a .hpp (and some others) for C++ header files to differentiate them from C (or shared C/C++) header files.

Share this post


Link to post
Share on other sites
There is no cconio because conio.h doesn't belong to the c++ standard and the c standard.
As already said, all c++ standard headers have no suffix, but the c header files have a c in the beginning, for example the c-header stdio.h is in c++ called cstdio and its members are in the namespace std.

You should give your own header files always a suffix like .h or .hpp

Share this post


Link to post
Share on other sites
Quote:
Original post by mike74
This is sort of a trivial question, but it's been perplexing me. What determines whether you need to put a .h at the end of an include in C++? For instance:

#include <cstdlib>
#include <iostream>
#include <hash_map.h>
#include <conio.h>

Why is it that #include <hash_map> doesn't work? Or #include <conio>? I would think both of these headers are fairly standard.


You're confusing "standard headers" with "headers that are fairly standard". The former, when used on gamedev.net, usually refers to headers defined by the C++ standard (a "de jure" standard. That is, an official standard). These include cstdlib and iostream. conio.h is a "fairly standard" (or "de facto" standard) header. It's only standard in the sense that a lot of compilers have it.

So, why does conio.h have a .h while C++ standard headers don't? It's just a naming convention. conio.h and iostream are just names of files. The .h makes the files easier to identify in a directory, so people usually put a .h on their headers. The C++ standards committee decided to leave off the .h because they don't technically have to exist as files (the compiler is free to use whatever magic it pleases when it sees "#include <iostream>" so long as it gets the job done).

So, why do you sometimes see iostream.h? Because, before it became part of the C++ standard (de jure standard) it was a fairly standard header (de facto standard). When it made the switch, the name changed to follow the new naming convention (i.e. to not have a .h).

Share this post


Link to post
Share on other sites
Quote:
Original post by Way Walker
So, why do you sometimes see iostream.h? Because, before it became part of the C++ standard (de jure standard) it was a fairly standard header (de facto standard). When it made the switch, the name changed to follow the new naming convention (i.e. to not have a .h).

I have to add a bit:
they introduced the new naming convention to allow compilers to offer still the old header files (without namespaces and other new stuff) because there was already a lot of code written using them.

Share this post


Link to post
Share on other sites
Quote:
Original post by mike74
What determines whether you need to put a .h at the end of an include in C++?


The answer is this:
  • The name you use is the name of the file.


For example, if you want to include the file named foo.h, you use "foo.h" or <foo.h>, not <foo> because that is not the name of the file. If you want to include the file named bar, you use "bar" or <bar>, not "bar.h" because that is not the name of the file.

Now, why do some files have ".h" in the name and other don't? The answer is that it was decided that the names of C++ Standard Library files would not have the extension ".h". I'm sure there is some reason for this, but I don't know what it is.

Finally, what about "stdio.h" and "cstdio"? Basically, most (if not all) C Standard Library header files have equivalent C++ Standard Library header files. If you are using C, you should include the file "stdio.h" (for example), and if you are using C++, you should include the file "cstdio" (for example).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!