At school, our teacher always taught us to use .hpp (C++ Header files) with our .cpp (C++ files). But Arduino seems to use .h files (with .cpp links?).

When someone is programming in C (without C++ compiler) they have the chance to get linked to a .cpp file through a .h file?

  • So why isn't a .cpp file always with a .hpp file?
  • What are the drawbacks of using .hpp files with your .cpp files?

This is not to start a discussion on what's best, please provide an answer with arguments that are not based on "what you like to do more".


The extension you use for a header file doesn't technically matter because the compiler never sees it. You could name it with the extension .this-is-a-header-file and it would probably still work.

The compiler only handles the C/C++ source files, typically named .c and .cpp. However, before that happens, the preprocessor goes through and looks for any #include lines. When it sees one, it basically copies the entire contents of the included file into the source file before it gets compiled.

This is normally used to include header files. However, the preprocessor doesn't know or care about different programming languages. All it does is process the raw text so there's nothing stopping you from including any text file you like, with any name or extension.

Choosing to use .h or .hpp is mostly just personal preference. You could certainly argue that it lets you (as a programmer) distinguish between C/C++ code more easily.

However, the reason why .h is more common is probably that C and C++ code is mixed so freely. You can generally compile C code as C++ without any problems, in which case it's the context (rather than the file itself) that tells you if it's C or C++. It's also possible to write a library in C++ which only exposes a C interface, meaning it could be used by C programs.

You could definitely include a C++-specific header file in a C program by mistake. All that would happen is that the compiler wouldn't understand the contents of that header file. Sensible development environments don't link to a project/library when it sees a #include directive (the Arduino IDE is the only one which does this, as far as I'm aware).

  • So there isn't really a standard for this? Or well, it defines that it doesn't really matter wethether it's a .hpp or .h... interesting... – Paul Apr 9 '15 at 13:54
  • 3
    Also .cxx and .hxx. I've seen those too. Because they look like pluses that fell over, or something. – Mark Allen Apr 9 '15 at 22:22
  • 2
    It doesn't matter which one you use in most cases, but there are conventions. For example, many libraries have both C and C++ bindings. You can't put the C++ syntax into a C header, because it wont compile. In such situations, it is common to have the .h file contain the C header, and a matching .hpp containing the C++ interface (which often, includes the .h and then wraps it in more C++ friendly notation) – Cort Ammon Apr 9 '15 at 22:42
  • 1
    @MarkAllen using xx instead of ++ seems to prevent problem with special char is some obscure fs. Maybe there is no real problem, but someone decided to not risk. – Lesto Apr 11 '15 at 8:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.