Take the 2-minute tour ×
Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. It's 100% free, no registration required.

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 (withouth 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".

share|improve this question

1 Answer 1

up vote 11 down vote accepted

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).

share|improve this answer
    
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... –  FuaZe 2 days ago
    
Also .cxx and .hxx. I've seen those too. Because they look like pluses that fell over, or something. –  Mark Allen 2 days ago
    
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 2 days ago
    
@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 15 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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