H Or *.hpp For Your Class Definitions - C++ - Stack Overflow
Có thể bạn quan tâm
-
- Home
- Questions
- Tags
- Users
- Companies
- Labs
- Jobs
- Discussions
- Collectives
-
Communities for your favorite technologies. Explore all Collectives
- Teams
Ask questions, find answers and collaborate at work with Stack Overflow for Teams.
Try Teams for free Explore Teams - Teams
-
Ask questions, find answers and collaborate at work with Stack Overflow for Teams. Explore Teams
Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about CollectivesTeams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about TeamsGet early access and see previews of new features.
Learn more about Labs *.h or *.hpp for your class definitions Ask Question Asked 16 years, 3 months ago Modified 7 months ago Viewed 520k times 775I've always used a *.h file for my class definitions, but after reading some boost library code, I realised they all use *.hpp. I've always had an aversion to that file extension, I think mainly because I'm not used to it.
What are the advantages and disadvantages of using *.hpp over *.h?
Share Improve this question Follow edited Jul 20, 2016 at 19:20 gsamaras 73.3k48 gold badges205 silver badges324 bronze badges asked Sep 30, 2008 at 10:47 Mark IngramMark Ingram 73.5k53 gold badges177 silver badges233 bronze badges 2- 2 Meanwhile there are The C++ Core Guidelines which unambiguously recommend *.h – Christophe Commented Jan 5, 2022 at 0:45
- 3 @Christophe I wouldn't say the standard "unambiguously" recommends *.h. They leave it open to project convention: SF.1: Use a .cpp suffix for code files and .h for interface files if your project doesn’t already follow another convention – PaulD Commented Jul 29, 2022 at 16:15
22 Answers
Sorted by: Reset to default Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first) 741Here are a couple of reasons for having different naming of C vs C++ headers:
- Automatic code formatting, you might have different guidelines for formatting C and C++ code. If the headers are separated by extension you can set your editor to apply the appropriate formatting automatically
- Naming, I've been on projects where there were libraries written in C and then wrappers had been implemented in C++. Since the headers usually had similar names, i.e. Feature.h vs Feature.hpp, they were easy to tell apart.
- Inclusion, maybe your project has more appropriate versions available written in C++ but you are using the C version (see above point). If headers are named after the language they are implemented in you can easily spot all the C-headers and check for C++ versions.
Remember, C is not C++ and it can be very dangerous to mix and match unless you know what you are doing. Naming your sources appropriately helps you tell the languages apart.
Share Improve this answer Follow answered Sep 30, 2008 at 11:41 David HolmDavid Holm 17.9k8 gold badges49 silver badges47 bronze badges 0 Add a comment | 343I use .hpp because I want the user to differentiate what headers are C++ headers, and what headers are C headers.
This can be important when your project is using both C and C++ modules: Like someone else explained before me, you should do it very carefully, and its starts by the "contract" you offer through the extension
.hpp : C++ Headers(Or .hxx, or .hh, or whatever)
This header is for C++ only.
If you're in a C module, don't even try to include it. You won't like it, because no effort is done to make it C-friendly (too much would be lost, like function overloading, namespaces, etc. etc.).
.h : C/C++ compatible or pure C HeadersThis header can be included by both a C source, and a C++ source, directly or indirectly.
It can included directly, being protected by the __cplusplus macro:
- Which mean that, from a C++ viewpoint, the C-compatible code will be defined as extern "C".
- From a C viewpoint, all the C code will be plainly visible, but the C++ code will be hidden (because it won't compile in a C compiler).
For example:
#ifndef MY_HEADER_H #define MY_HEADER_H #ifdef __cplusplus extern "C" { #endif void myCFunction() ; #ifdef __cplusplus } // extern "C" #endif #endif // MY_HEADER_HOr it could be included indirectly by the corresponding .hpp header enclosing it with the extern "C" declaration.
For example:
#ifndef MY_HEADER_HPP #define MY_HEADER_HPP extern "C" { #include "my_header.h" } #endif // MY_HEADER_HPPand:
#ifndef MY_HEADER_H #define MY_HEADER_H void myCFunction() ; #endif // MY_HEADER_H Share Improve this answer Follow edited May 21, 2018 at 15:52 CommunityBot 11 silver badge answered Sep 30, 2008 at 18:57 paercebalpaercebal 83.2k38 gold badges134 silver badges160 bronze badges 4- 12 This is quite uncustomary in many C++ projects. .h files are used for very un-C'ish stuff. – einpoklum Commented Mar 11, 2016 at 21:45
- 22 @einpoklum : Of course. But I try to avoid the "Monkey See Monkey Do" behavior. In the current case, both extensions (and others) are available, so I try to make them actually count. Having this contract with code that is shared with clients is very useful: Everyone (i.e. hundreds of developers) knows that ".H" files are to be used by clients using a C compiler, so there is no mistaking about what can go there or not. And every one (including the clients) know that ".HPP" files will never attempt to be C-friendly. Everyone wins. – paercebal Commented Mar 13, 2016 at 6:17
- 6 @paercebal, so you're suggesting .H instead of .h, and .HPP over .hpp? – Geof Sawaya Commented Aug 19, 2016 at 0:33
- 13 @GeofSawaya : No, sorry. It's an habit. When writing articles, I use extensions in uppercase to differentiate files by their type, like ".HPP files". But the extensions of the actual files that are on my hard disk are always in lowercase, even in the name is not, like "MyClass.hpp" or "module.hpp" – paercebal Commented Aug 19, 2016 at 22:54
I always considered the .hpp header to be a sort of portmanteau of .h and .cpp files...a header which contains implementation details as well.
Typically when I've seen (and use) .hpp as an extension, there is no corresponding .cpp file. As others have said, this isn't a hard and fast rule, just how I tend to use .hpp files.
Share Improve this answer Follow edited Jul 18, 2016 at 9:22 mtb 1,37016 silver badges32 bronze badges answered Sep 30, 2008 at 14:28 PerculatorPerculator 1,3312 gold badges10 silver badges12 bronze badges Add a comment | 53It does not matter which extension you use. Either one is OK.
I use *.h for C and *.hpp for C++.
Share Improve this answer Follow edited Jul 18, 2016 at 9:07 mtb 1,37016 silver badges32 bronze badges answered Sep 30, 2008 at 10:48 BurkhardBurkhard 14.7k22 gold badges90 silver badges113 bronze badges Add a comment | 50EDIT [Added suggestion from Dan Nissenbaum]:
By one convention, .hpp files are used when the prototypes are defined in the header itself. Such definitions in headers are useful in case of templates, since the compiler generates the code for each type only on template instantiation. Hence, if they are not defined in header files, their definitions will not be resolved at link time from other compilation units. If your project is a C++ only project that makes heavy use of templates, this convention will be useful.
Certain template libraries that adhere to this convention provide headers with .hpp extensions to indicate that they do not have corresponding .cpp files.
another convention is to use .h for C headers and .hpp for C++; a good example would be the boost library.
Share Improve this answer Follow edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge answered Dec 3, 2013 at 12:08 ProgramCppProgramCpp 1,3902 gold badges16 silver badges29 bronze badges 3Quote from Boost FAQ,
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)
- None of that is true, it makes no difference whether the file is .h or .hpp when it comes to code generation or linking. – Mark Ingram Commented Dec 3, 2013 at 14:01
- isn't it just a matter of convention? C++ std library provides all its headers without any extension. using ".hpp" just indicates that the prototypes are defined in the same file and there will not be any corresponding .cpp file. – ProgramCpp Commented Dec 4, 2013 at 4:36
- 19 This answer is useful, I think, with the exception that it's missing a very simple, but important, phrase: "by convention, not by the rules of the language" (somewhere). – Dan Nissenbaum Commented Dec 15, 2014 at 17:39
I've recently started using *.hpp for c++ headers.
The reason is that I use emacs as my primary editor and it enters automatically into c-mode when you load a *.h file and into c++-mode when you load a *.hpp file.
Apart that fact I see no good reasons for choosing *.h over *.hpp or vice-versa.
Share Improve this answer Follow edited Jul 18, 2016 at 9:22 mtb 1,37016 silver badges32 bronze badges answered Sep 30, 2008 at 11:28 SergeSerge 7,6945 gold badges41 silver badges46 bronze badges 1- 10 Personally, I think C++ highlighting is a good idea even in C headers. I have been on both ends of the situation where someone wants to include your C header from C++, but it uses a C++ keyword as a parameter name... – Steve Jessop Commented Sep 30, 2008 at 12:07
I'm answering this as an reminder, to give point to my comment(s) on "user1949346" answer to this same OP.
So as many already answered: either way is fine. Followed by emphasizes of their own impressions.
Introductory, as also in the previous named comments stated, my opinion is C++ header extensions are proposed to be .h if there is actually no reason against it.
Since the ISO/IEC documents use this notation of header files and no string matching to .hpp even occurs in their language documentations about C++.
But I'm now aiming for an approvable reason WHY either way is ok, and especially why it's not subject of the language it self.
So here we go.
The C++ documentation (I'm actually taking reference from the version N3690) defines that a header has to conform to the following syntax:
2.9 Header names
header-name: < h-char-sequence > " q-char-sequence " h-char-sequence: h-char h-char-sequence h-char h-char: any member of the source character set except new-line and > q-char-sequence: q-char q-char-sequence q-char q-char: any member of the source character set except new-line and "
So as we can extract from this part, the header file name may be anything that is valid in the source code, too. Except containing '\n' characters and depending on if it is to be included by <> it is not allowed to contain a >. Or the other way if it is included by ""-include it is not allowed to contain a ".
In other words: if you had a environment supporting filenames like prettyStupidIdea.>, an include like:
#include "prettyStupidIdea.>"would be valid, but:
#include <prettyStupidIdea.>>would be invalid. The other way around the same.
And even
#include <<.<>would be a valid includable header file name.
Even this would conform to C++, it would be a pretty pretty stupid idea, tho.
And that's why .hpp is valid, too.
But it's not an outcome of the committees designing decisions for the language!
So discussing about to use .hpp is same as doing it about .cc, .mm or what ever else I read in other posts on this topic.
I have to admit I have no clue where .hpp came from1, but I would bet an inventor of some parsing tool, IDE or something else concerned with C++ came to this idea to optimize some internal processes or just to invent some (probably even for them necessarily) new naming conventions.
But it is not part of the language.
And whenever one decides to use it this way. May it be because he likes it most or because some applications of the workflow require it, it never2 is a requirement of the language. So whoever says "the pp is because it is used with C++", simply is wrong in regards of the languages definition.
C++ allows anything respecting the previous paragraph. And if there is anything the committee proposed to use, then it is using .h since this is the extension sued in all examples of the ISO document.
Conclusion:
As long you don't see/feel any need of using .h over .hpp or vise versa, you shouldn't bother. Because both would be form a valid header name of same quality in respect to the standard. And therefore anything that REQUIRES you to use .h or .hpp is an additional restriction of the standard which could even be contradicting with other additional restrictions not conform with each other. But as OP doesn't mention any additional language restriction, this is the only correct and approvable answer to the question
"*.h or *.hpp for your class definitions" is:
Both are equally correct and applicable as long as no external restrictions are present.
1From what I know, apparently, it is the boost framework that came up with that .hpp extension.
2Of course I can't say what some future versions will bring with it!
Share Improve this answer Follow edited Dec 14, 2018 at 6:47 Pang 10.1k146 gold badges85 silver badges124 bronze badges answered Mar 8, 2016 at 13:14 dheindhein 6,5615 gold badges44 silver badges79 bronze badges 1- I think I'll go with .hpp as I don't want to get sued by ISO – Paul Childs Commented Dec 29, 2023 at 3:37
You can call your includes whatever you like.
Just need to specify that full name in the #include.
I suggest that if you work with C to use .h and when with C++ to use .hpp.
It is in the end just a convention.
Share Improve this answer Follow edited Jul 18, 2016 at 9:22 mtb 1,37016 silver badges32 bronze badges answered Sep 30, 2008 at 10:51 slashmaisslashmais 7,1359 gold badges56 silver badges80 bronze badges Add a comment | 8Bjarne Stroustrup and Herb Sutter have a statement to this question in their C++ Core guidelines found on: https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#S-source which is also refering to the latest changes in the standard extension (C++11, C++14, etc.)
SF.1: Use a .cpp suffix for code files and .h for interface files if your Y project doesn't already follow another convention Reason
It's a longstanding convention. But consistency is more important, so if your project uses something else, follow that. Note
This convention reflects a common use pattern: Headers are more often shared with C to compile as both C++ and C, which typically uses .h, and it's easier to name all headers .h instead of having different extensions for just those headers that are intended to be shared with C. On the other hand, implementation files are rarely shared with C and so should typically be distinguished from .c files, so it's normally best to name all C++ implementation files something else (such as .cpp).
The specific names .h and .cpp are not required (just recommended as a default) and other names are in widespread use. Examples are .hh, .C, and .cxx. Use such names equivalently. In this document, we refer to .h and .cpp > as a shorthand for header and implementation files, even though the actual extension may be different.
Your IDE (if you use one) may have strong opinions about suffices.
I'm not a big fan of this convention because if you are using a popular library like boost, your consistency is already broken and you should better use .hpp.
Share Improve this answer Follow edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge answered Aug 13, 2017 at 8:32 ruunsruuns 1232 silver badges6 bronze badges Add a comment | 7I prefer .hpp for C++ to make it clear to both editors and to other programmers that it is a C++ header rather than a C header file.
Share Improve this answer Follow answered Sep 30, 2008 at 14:15 JohnMcGJohnMcG 8,7956 gold badges43 silver badges49 bronze badges Add a comment | 7Fortunately, it is simple.
You should use the .hpp extension if you're working with C++ and you should use .h for C or mixing C and C++.
Share Improve this answer Follow answered Sep 5, 2017 at 11:51 NykalNykal 1692 silver badges4 bronze badges Add a comment | 6Codegear C++Builder uses .hpp for header files automagically generated from Delphi source files, and .h files for your "own" header files.
So, when I'm writing a C++ header file I always use .h.
Share Improve this answer Follow answered Sep 30, 2008 at 10:53 RoddyRoddy 67.9k44 gold badges170 silver badges281 bronze badges Add a comment | 6In one of my jobs in the early 90's, we used .cc and .hh for source and header files respectively. I still prefer it over all the alternatives, probably because it's easiest to type.
Share Improve this answer Follow answered Sep 30, 2008 at 11:15 camhcamh 42.4k13 gold badges64 silver badges72 bronze badges 0 Add a comment | 6C++ ("C Plus Plus") makes sense as .cpp
Having header files with a .hpp extension doesn't have the same logical flow.
Share Improve this answer Follow answered Sep 30, 2008 at 15:18 DyniteDynite 2,3836 gold badges30 silver badges38 bronze badges 1- Good point. There is no h++. Hadn't though of that. – steve Commented Nov 14 at 12:43
It is easy for tools and humans to differentiate something. That's it.
In conventional use (by boost, etc), .hpp is specifically C++ headers. On the other hand, .h is for non-C++-only headers (mainly C). To precisely detect the language of the content is generally hard since there are many non-trivial cases, so this difference often makes a ready-to-use tool easy to write. For humans, once get the convention, it is also easy to remember and easy to use.
However, I'd point out the convention itself does not always work, as expected.
- It is not forced by the specification of languages, neither C nor C++. There exist many projects which do not follow the convention. Once you need to merge (to mix) them, it can be troublesome.
- .hpp itself is not the only choice. Why not .hh or .hxx? (Though anyway, you usually need at least one conventional rule about filenames and paths.)
I personally use both .h and .hpp in my C++ projects. I don't follow the convention above because:
- The languages used by each part of the projects are explicitly documented. No chance to mix C and C++ in same module (directory). Every 3rdparty library is required to conforming to this rule.
- The conformed language specifications and allowed language dialects used by the projects are also documented. (In fact, I even document the source of the standard features and bug fix (on the language standard) being used.) This is somewhat more important than distinguishing the used languages since it is too error-prone and the cost of test (e.g. compiler compatibility) may be significant (complicated and time-consuming), especially in a project which is already in almost pure C++. Filenames are too weak to handle this.
- Even for the same C++ dialect, there may be more important properties suitable to the difference. For example, see the convention below.
- Filenames are essentially pieces of fragile metadata. The violation of convention is not so easy to detect. To be stable dealing the content, a tool should eventually not only depend on names. The difference between extensions is only a hint. Tools using it should also not be expected behave same all the time, e.g. language-detecting of .h files on github.com. (There may be something in comments like shebang for these source files to be better metadata, but it is even not conventional like filenames, so also not reliable in general.)
I usually use .hpp on C++ headers and the headers should be used (maintained) in a header-only manner, e.g. as template libraries. For other headers in .h, either there is a corresponding .cpp file as implementation, or it is a non-C++ header. The latter is trivial to differentiate through the contents of the header by humans (or by tools with explicit embedded metadata, if needed).
Share Improve this answer Follow edited Jan 30, 2016 at 1:27 answered Jan 30, 2016 at 1:20 FrankHBFrankHB 2,49528 silver badges21 bronze badges 1- I've happily #incude-d *.c files, files with no extension, ... Famously, X11 (ab)used the C preprocessor to generate Makefiles. – vonbrand Commented Aug 10 at 17:09
There is no advantage to any particular extension, other than that one may have a different meaning to you, the compiler, and/or your tools. header.h is a valid header. header.hpp is a valid header. header.hh is a valid header. header.hx is a valid header. h.header is a valid header. this.is.not.a.valid.header is a valid header in denial. ihjkflajfajfklaf is a valid header. As long as the name can be parsed properly by the compiler, and the file system supports it, it's a valid header, and the only advantage to its extension is what one reads into it.
That being said, being able to accurately make assumptions based on the extension is very useful, so it would be wise to use an easily-understandable set of rules for your header files. Personally, I prefer to do something like this:
- If there are already any established guidelines, follow them to prevent confusion.
- If all source files in the project are for the same language, use .h. There's no ambiguity.
- If some headers are compatible with multiple languages, while others are only compatible with a single language, extensions are based on the most restrictive language that a header is compatible with. A header compatible with C, or with both C & C++, gets .h, while a header compatible with C++ but not C gets .hpp or .hh or something of the sort.
This, of course, is but one of many ways to handle extensions, and you can't necessarily trust your first impression even if things seem straightforward. For example, I've seen mention of using .h for normal headers, and .tpp for headers that only contain definitions for templated class member functions, with .h files that define templated classes including the .tpp files that define their member functions (instead of the .h header directly containing both the function declaration and the definition). For another example, a good many people always reflect the header's language in its extension, even when there's no chance of ambiguity; to them, .h is always a C header and .hpp (or .hh, or .hxx, etc.) is always a C++ header. And yet again, some people use .h for "header associated with a source file" and .hpp for "header with all functions defined inline".
Considering this, the main advantage would come in consistently naming your headers in the same style, and making that style readily apparent to anyone examining your code. This way, anyone familiar with your usual coding style will be able to determine what you mean with any given extension with just a cursory glance.
Share Improve this answer Follow answered Jun 15, 2016 at 3:50 Justin Time - Reinstate MonicaJustin Time - Reinstate Monica 4,31926 silver badges42 bronze badges Add a comment | 4As many here have already mentioned, I also prefer using .hpp for header-only libraries that use template classes/functions. I prefer to use .h for header files accompanied by .cpp source files or shared or static libraries.
Most of the libraries I develop are template based and thus need to be header only, but when writing applications I tend to separate declaration from implementation and end up with .h and .cpp files
Share Improve this answer Follow answered Aug 18, 2017 at 23:42 EnzoEnzo 1,0627 silver badges7 bronze badges Add a comment | 3I use .h because that's what Microsoft uses, and what their code generator creates. No need to go against the grain.
Share Improve this answer Follow answered Sep 30, 2008 at 14:26 Mark RansomMark Ransom 308k44 gold badges416 silver badges647 bronze badges 5- not everywhere, in a lot of examples (e.q. WINDDK) they use .hpp – marsh-wiggle Commented Jul 21, 2014 at 13:03
- 20 I wouldn't call Microsoft 'the grain' when in comes to C++. – brettwhiteman Commented Aug 17, 2014 at 10:35
- 1 @Brett it is when that's your job. And even if it's not, it's a darn popular compiler. – Mark Ransom Commented Aug 19, 2014 at 4:54
- @MarkRansom I was more referring to Microsoft's history of using C. IMO VC++ is an excellent compiler. – brettwhiteman Commented Aug 20, 2014 at 6:44
- 1 @developerbmw I'd say MSVC is the grain for Windows programs, and GCC is the grain for *nix programs, personally. They're the ones most other compilers for those platforms tend to try to remain compatible with, to my knowledge. – Justin Time - Reinstate Monica Commented Jun 15, 2016 at 3:25
In "The C++ Programming Language, Third Edition by Bjarne Stroustrup", the nº1 must-read C++ book, he uses *.h. So I assume the best practice is to use *.h.
However, *.hpp is fine as well!
Share Improve this answer Follow answered May 29, 2013 at 16:25 user1949346user1949346 4- 1 If some one wrote a book about something he learned from others who learned it from another (maybe with luck the same) book, written by someone who maybe had the primary source available, then it is what they are doing anything but not the mark for beeing best practice. – dhein Commented Mar 8, 2016 at 10:50
- Just to mention: I actually dowvoted this. But I would have upvoted it, if it said "ISO/IEC N3690" or any other of the C++ drafts isntead of "The C++ Programming Language, Third Edition by Bjarne Stroustrup". While this would have been a valid point, since there is no mention of .hpp by the language itself at all. – dhein Commented Mar 8, 2016 at 10:53
- 13 @zaibis you do know that Bjarne Stroustrup invented C++ right? – tumtumtum Commented Jun 13, 2016 at 10:08
- @tumtumtum: admited, I wasn't aware of this. But anyway, even in that case, its still the document of the comittee, maintaining the standard, which is the refference to take. Even if he's the inventor of the language, it isn't his decision anymore. So, while this makes this answer more valluable, its still not a valid reasoning. – dhein Commented Jun 13, 2016 at 10:12
The extension of the source file may have meaning to your build system, for example, you might have a rule in your makefile for .cpp or .c files, or your compiler (e.g. Microsoft cl.exe) might compile the file as C or C++ depending on the extension.
Because you have to provide the whole filename to the #include directive, the header file extension is irrelevant. You can include a .c file in another source file if you like, because it's just a textual include. Your compiler might have an option to dump the preprocessed output which will make this clear (Microsoft: /P to preprocess to file, /E to preprocess to stdout, /EP to omit #line directives, /C to retain comments)
You might choose to use .hpp for files that are only relevant to the C++ environment, i.e. they use features that won't compile in C.
Share Improve this answer Follow answered Sep 30, 2008 at 11:34 Mike DimmickMike Dimmick 9,7922 gold badges26 silver badges48 bronze badges 1- I was looking for the answer that talked about build systems. Having written a "build system" on top of make(1), the file extension is a convenient way to guide the build process. My build system was such that you did not need separate header and source files for services/classes, and although not necessary, we adopted the extension ".ch" for such files, ".cch" for C++. Personally, I loathe ".cpp" as that extension was used by UNIX for "C pre-processor" output; ".cc" was the extension of favor. But when you control the build system, you can use anything. – user3481644 Commented Jan 7 at 13:55
In our C++ system, we use .h and .cpp as the C++ headers and source files, with all definitions and implementations separated. (Since we are a pure C++ system there's no C code mixed inside)
However, there's a special case for template (or generic) classes, which need to be defined inside headers due to syntactical limitations.
Therefore, we use .hpp files to store the complex logic associated with these template classes while keeping the interface definitions in the .h files. This allows us to maintain a clear separation of declarations and implementations while accommodating the requirements of template classes.
Anyway, syntactically, any file extension is feasible, what's more important is maintaining consistency with the team’s style.
Share Improve this answer Follow answered May 15 at 15:25 AnonymousXAnonymousX 1,00811 silver badges10 bronze badges Add a comment | 0The .h, .hpp, .hxx files are all treated identically by the compiler. I consider .hxx headers to be in between the .h header file and the .cxx source file.
The cxx files contain definitions of the non-templated functions. Their type-info is well known. They can be compiled and distributed as binary library files (.a or .so).
On the other hand, the hxx files contain definitions of the templated functions. Their type-info is not known beforehand. Thus they are distributed as source code.
The following example demonstrates my convention.
File: my_math.h
#ifndef my_math_h #define my_math_h class MyMath { public: static double add (double a, double b); template<class in1_type, class in2_type, class res_type> static res_type mult (in1_type in1, in2_type in2); }; #endifFile: my_math.hxx
#ifndef my_math_hxx #define my_math_hxx #include "my_math.h" template<class in1_type, class in2_type, class res_type> res_type MyMath::mult (in1_type in1, in2_type in2) { return in1 * in2; } #endifFile: my_math.cxx
#include "my_math.h" double MyMath::add (double a, double b) { return a + b; }Compilation commands using gcc
g++ -fPIC -c my_math.cxx -o my_math.o g++ -shared my_math.o -o my_math.soFile: main.cxx
#include "my_math.h" #include "my_math.hxx" int main (void) { double a = 10, b = 20; double c = MyMath::add (a, b); // Definition comes from .so file int x = 10; float y = 20; double z = MyMath<double>::mult (x, y); // Definition comes from .hxx file return 0; }Compilation commands using GCC
g++ main.cxx my_math.so -o mainSomeone may define the templated mult function as inline inside the class declaration in the .h file. That is ok for a one-liner. Multi-line template functions make the class declaration unreadable.
Share Improve this answer Follow answered Mar 27 at 12:14 KrishnenduKrishnendu 1265 bronze badges Add a comment | Highly active question. Earn 10 reputation (not counting the association bonus) in order to answer this question. The reputation requirement helps protect this question from spam and non-answer activity.Not the answer you're looking for? Browse other questions tagged
or ask your own question.- The Overflow Blog
- The real 10x developer makes their whole team better
- The ghost jobs haunting your career search
- Featured on Meta
- The December 2024 Community Asks Sprint has been moved to March 2025 (and...
- Stack Overflow Jobs is expanding to more countries
Linked
332 .c vs .cc vs. .cpp vs .hpp vs .h vs .cxx -4 What is the difference and what affects the performance of using .hpp or .h files in c++? 26 #include C++ header files in opencv 14 what's the difference between hpp and hxx? 20 When to use .hpp files 7 Is there any reason Google uses the cc extension instead of cpp in their open source projects? 4 Including hpp files in an R package -1 How to hide member function file while sharing class 0 How do I fix xcode lexical or preprocessor issue, array file not found in xcode 6.3? 1 How to use classes from a Test project in another Test Project in VC++? See more linked questionsRelated
7 What is the difference between header (.h) and source (.cpp) files? 1 Header files. etc 2 Class header files 0 How to use header files for classes? 3 Including header files in C++ (class definition and method implementation) 2 Using classes in .h files 0 Class declarations in header files 2 Class declaration and implementation in the header file 4 When to create header file only classes 0 C++ classes in Header FilesHot Network Questions
- Confusing usage of 「これ」 (with an unclear referent) and 「の」 (which could be possesive or appositional)
- Did the Israelites defecate when eating the manna?
- 2012 vs 2022 Chevrolet Vehicle and Coolant Consumption
- Mr. Potter in "It's a Wonderful Life"
- Countable translations of a positive measure set cover an interval?
- Rust implementation of Euler problems
- Extract signer information from portable executable (PE)
- Loop over array cyclically
- In what sense bootstrapping allows you to bypass certain assumptions of the linear regression method?
- Sous vide pouches puffed up - Is this product contaminated?
- Conditional anchor positioning based on pgf keys
- algorithm2e change color of colon (:) after Input/Output
- Double factorial power series closed form expression
- Issues with Implementing Adaptive Step-Size Explicit Runge-Kutta Methods in C for ODE Solvers
- Is there an MVP or "Hello world" for chess programming?
- Where did Tolstoy write that a man is like a fraction?
- How energy conservation works in conserved angular momentum scenerio?
- How to make the spacing between these circles consistent?
- Who is this man being noticed by Robert in this scene?
- How to place a heavy bike on a workstand without lifting
- How to limit width of a cell in an array?
- What's the name of the form of the song "12 Days of Christmas"?
- If I have a multiannual stay-permit in France, will they still check biometrics on arrival at the airport?
- Mindcrime feat. Spielberg
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
lang-cppTừ khóa » .h .hpp
-
Why .h Is More Widely Used Than .hpp For C++ Header Files - Reddit
-
Thoughts On Header File Extensions: .h Vs .hpp - Embedded Artistry
-
What Is Difference Between ".h", ".hpp" And "header File Not Having ...
-
HPP File Extension - What Is .hpp And How To Open? - ReviverSoft
-
C++ Header File (*.h, *.hpp) - RAD Studio - Embarcadero DocWiki
-
Add Command To Switch Between Header (.h, .hpp) And ...
-
Why Don't C++ Standard Headers' Names End With '.h' Or '.hpp'? - Quora
-
The Difference Between Hpp And H - Actorsfit
-
HPP – The C++ Header File - Online Converter
-
Consider .hpp And .hxx Files To Be Header Files Too (C++) · Issue #1079
-
[DEBT] Rename .h|.hpp|.cuh|.cu|.cpp Files Properly In Our Cuml C++ ...
-
What Is The Difference Between .h And .HPP Files? - AnswersToAll
-
The Difference Between H, Cpp And Hpp Header Files - Karthi Softek