Understanding of .hpp in C++

What is hpp file

        In the past, when writing C++ programs, if you need to sub-modules, we usually write a .h file, put the declaration of the function in it, and then write a .cpp file, and add the relevant implementation of the function in it, so when you use it Include the header file, and then add .cpp to the current project to compile. If you forget to add .cpp to the current project to compile at this time, the function will often not be found. If your .h is the declaration of the template class and .cpp is the realization of the template class function, because the template is compiled twice, the function will not be found if you directly include the header file. In this case, you need to implement and declare Put them together to find the definition of the function.

        The hpp (Header Plus Plus) header file, as the name implies, is the .h file plus the .cpp file, which frequently appears in the boost open source library. In fact, the .cpp implementation code is mixed into the .h file, and the definition and implementation are contained in the same file .

 

Benefits of using hpp files

1. The hpp file puts the definition and implementation in the same file, reducing the number of files

2. No need to add cpp to the project to compile, compile the code directly to the caller's obj file, no longer generate a separate obj, greatly reduce the number of compilations, very suitable for writing open source libraries

3. The boost library uses a lot of templates, and the form of sampling hpp can maintain better compatibility with various compilers (C++ templates cannot separate the source file and the declaration file into two files)

Guess you like

Origin blog.csdn.net/m0_37957160/article/details/109116047