Thoughts On Header File Extensions: .h Vs .hpp - Embedded Artistry

24 April 2017 by Phillip Johnston • Last updated 21 April 2020

I’m creating a new framework which uses a mix of C and C++, and recently I stopped to consider my choice of header file extension.

I have spent my career as a C programmer, so many of my habits are simply ossified from my C days. One such habit is using .h for all header file extensions.

To make matters worse, it seems there is still not a fixed approach to C/C++ header file differentiation. While surveying comments around the internet, I see many developers only use .hpp for C++ headers which also include implementations. Other C++-only headers are left with the .h extension. Unhelpfully, many IDEs still seem to recommend .h as the default header extension, even if you are using C++.

The problem with allowing .h to represent both C and C++ code without differentiation is that you increase friction for developers:

  • How can you know that you can’t include a specific library .h file in your C program because it’s really C++?
  • What if you are trying to adjust your build system and want to exclude all C++ files from a specific project?

I think it is important to make conventions and language type apparent to developers without requiring manual inspection. In my new framework, I have decided to use .hpp for all headers that are C++-only. .h will be reserved for headers which can be included by either C or C++ sources. I’m not alone in this convention – at the very least, Boost uses the same approach. To quote from the Boost FAQ:

Why do Boost headers have a .hpp suffix rather than .h or none at all? File extensions communicate the “type” of the file, both to humans and to computer programs. The ‘.h’ extension is used for C header files, and therefore communicates the wrong thing about C++ header files. Using no extension communicates nothing and forces inspection of file contents to determine type. Using ‘.hpp’ unambiguously identifies it as C++ header file, and works well in actual practice. (Rainer Deyke)

As an aside, it is helpful to make your C-style headers compatible with C++. Here are some simple strategies to keep in mind:

  • protect C-only code with the __cplusplus macro, which is only defined by the C++ compiler
  • Mark C functions code as extern "C" when compiling C++ code

I hope that you take some time to think about header extensions in your own projects!

Migrating from C to C++ Articles

Embedded C++: What’s the value?

22 July 2016 / Phillip Johnston / Cpp, Embedded C++ Series, Lessons from the Field, Migrating from C to C++

Migrating to Modern C++

28 December 2016 / Phillip Johnston / Cpp, Migrating from C to C++

C++ Smart Pointers

4 January 2017 / Phillip Johnston / C, Migrating from C to C++

std::shared_ptr and shared_from_this

11 January 2017 / Phillip Johnston / Cpp, Migrating from C to C++

Virtual Function Defaults

25 January 2017 / Phillip Johnston / Cpp, Migrating from C to C++

C++ Smart Pointers with Aligned Malloc/Free

1 March 2017 / Phillip Johnston / Cpp, Embedded C++ Series, Featured, Memory, Migrating from C to C++

Migrating from C to C++: NULL vs nullptr

8 March 2017 / Phillip Johnston / Cpp, Migrating from C to C++

Never Call Virtual Functions During Construction or Destruction

13 March 2017 / Phillip Johnston / Cpp, Migrating from C to C++, Monday Morning Reading, Reading Material

C++ Casting, or: “Oh No, They Broke Malloc!”

15 March 2017 / Phillip Johnston / C, Migrating from C to C++

Thoughts on the Vagaries of C++ Initialization

20 March 2017 / Phillip Johnston / Cpp, Migrating from C to C++, Monday Morning Reading, Reading Material

Thoughts on Header File Extensions: .h vs .hpp

24 April 2017 / Phillip Johnston / Cpp, Lessons from the Field, Migrating from C to C++

Mixing C and C++: extern C

1 May 2017 / Phillip Johnston / C, Cpp, Migrating from C to C++

An Introduction to std::vector

21 June 2017 / Phillip Johnston / Cpp, Migrating from C to C++

C++: Range-Based For Loops

23 June 2017 / Phillip Johnston / Cpp, Migrating from C to C++

An Introduction to std::array

28 June 2017 / Phillip Johnston / Cpp, Migrating from C to C++

Ditch Those Built-in Arrays for C++ Containers

7 July 2017 / Phillip Johnston / Cpp, Featured, Migrating from C to C++

Using A C++ Object’s Member Function with C-style Callbacks

10 July 2017 / Phillip Johnston / Cpp, Featured, Migrating from C to C++

Ditch Your C-style Pointers for Smart Pointers

12 July 2017 / Phillip Johnston / Cpp, Featured, Migrating from C to C++

Comparing Variadic Functions: C vs C++

14 July 2017 / Phillip Johnston / C, Cpp, Migrating from C to C++

Migrating from C to C++: Take Advantage of RAII/SBRM

17 July 2017 / Phillip Johnston / Cpp, Migrating from C to C++

C++: How to Utilize SBRM for C-style Interfaces and Resources

19 July 2017 / Phillip Johnston / Cpp, Migrating from C to C++

std::string vs C-strings

26 July 2017 / Phillip Johnston / Cpp, Featured, Migrating from C to C++

An Overview of C++ STL Containers

2 August 2017 / Phillip Johnston / Cpp, Featured, Migrating from C to C++

Choosing the Right STL Container: General Rules of Thumb

23 August 2017 / Phillip Johnston / Cpp, Featured, Migrating from C to C++, Rules of Thumb

Choosing the Right Container: Associative Containers

30 August 2017 / Phillip Johnston / Cpp, Featured, Migrating from C to C++, Rules of Thumb

Choosing the Right Container: Sequential Containers

11 September 2017 / Phillip Johnston / Cpp, Migrating from C to C++, Rules of Thumb

A Warning When Using -fno-exceptions

3 May 2018 / Phillip Johnston / Cpp, Embedded C++ Series, Exceptions, Migrating from C to C++, Tips and Tricks

nothrow new: the Variant to Use When Avoiding C++ Exceptions

10 May 2018 / Phillip Johnston / Cpp, Embedded C++ Series, Exceptions, Migrating from C to C++, Tips and Tricks

Remember to lock around all std::condition_variable “variables”

10 January 2022 / Phillip Johnston / C++, Migrating from C to C++

Related

Related Terms:
  • Glossary: C
  • Glossary: C++
  • Glossary: Build System
  • Glossary: Boost

Từ khóa » H Hpp Cpp