33

I would like to know why we use ".hh" as extension for C++ header files instead of using just ".h".

The header files are preprocessed and the preprocessor doesn't even care about the extension of the header file. So, even if I create a header file with an extension ".qwe" (test.qwe). Then, why to use ".hh" as extension for C++ header files.

Some say, we are using ".cc" as extension for C++ files to differentiate from C files (which has an extension ".c"), likewise we are using using ".hh" as extension for C++ header files to differentiate from C header files (which has an extension ".h"). I don't think this to be a valid reason.

Does anyone know the reason for naming in such a way?

vvvvv
  • 25,404
  • 19
  • 49
  • 81
veda
  • 6,416
  • 15
  • 58
  • 78
  • 4
    It's just a convention that some people use to differentiate headers intended for C++ programs from headers intended for C programs. Nothing more than that. Whether or not it's valid would largely boil down to religious wars similar to brace placement arguments. – Michael Burr Apr 27 '12 at 16:23
  • See also http://stackoverflow.com/q/5122728/716443 . It might be a clue for some compilers that your header is for Objective C++. – DavidO Apr 27 '12 at 16:25
  • 3
    `.hpp` is a popular choice for C++ headers but I just use `.h` and `.cpp`. This is basically personal preference. – AJG85 Apr 27 '12 at 16:26

6 Answers6

39

Some say, we are using ".cc" as extension for C++ files to differentiate from C files (which has an extension ".c"), likewise we are using using ".hh" as extension for C++ header files to differentiate from C header files (which has an extension ".h").

That is exactly the reason. It is just to differentiate CPP headers from C headers.

Some programmers and libraries, such as Boost, use .hpp for CPP headers. My personal choice is this:

  • example.c
  • example.cpp
  • example.h
  • example.h++

Even if they all belong to a huge project, you can still figure out which one is which. No description is needed.

Nawaz
  • 353,942
  • 115
  • 666
  • 851
8

You can name your headers as you wish, they can even have no extension at all.

This FAQ mentions header naming conventions.

mfontanini
  • 21,410
  • 4
  • 65
  • 73
  • The new link (same people, new page) is https://isocpp.org/wiki/faq/coding-standards#hdr-file-ext – quazgar Aug 04 '21 at 06:44
2

I think it is mostly a social convention.

However, recent GCC compilers are able to compile a (single) header foo.hh or foo.h into something like foo.hh.gch or foo.h.gch which essentially contains a persistent memory heap image of the compiler (the cc1plus program started by g++) after it had parsed that header.

Notice that the C++11 standard library defines header files like e.g. <vector> without any extension.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
1

It's not for any specific reason, it's just a (developer- or project-specific) convention. As "file extension" is just part of name, it doesn't matter much, as source files are just plain text files. Furthermore, header files are just copy-pasted into file that included them (via #include, so file extension mean even less for them.

Borland's libraries' headers had .hpp extension. C++'s standard library headers have no extension at all. It's just a matter of convention and personal taste.

Griwes
  • 8,805
  • 2
  • 43
  • 70
1

This is completely convention, there is no special reason for it. Usually people try to stay consistent across a project, or across teams or even an entire company. It helps developers who pick up your code after you're long gone understand that you abc.hh goes with file abc.cc and abc.h goes with abc.c.

drew212
  • 506
  • 3
  • 16
1

One reason, if I'm creating a dll that has a C API around a bunch of C++ code, I need to differentiate between my C headers which the client may use to import C functions and my internal C++ headers which I explicitly don't want to allow the client to import.

Binding to a C function is pretty simple from most languages/environments with almost no overhead. You don't have to think about all the complexities inherit in C++. So if I were to work with such a library, I'd be sure to separate my C++ from my C interface.

Doug T.
  • 64,223
  • 27
  • 138
  • 202