2

We define a C++ class in a .h and define its methods in a .cpp, but it makes the code look less organized.

I want to put all method's definition in the class definition which is in a .h file, but I'm worrying that the compiler generate duplicated code for the same methods/functions when one class header file is included by different files.

Does the linker find out and merge the duplicated code pieces to reduce the file size?

If not, is it better to use .hpp instead? I heard that a .hpp is for this.

And it does make minor difference when I just change a .h file for a .hpp (I don't know why), compiled with G++.

Keith Pinson
  • 7,835
  • 7
  • 61
  • 104
RnMss
  • 3,696
  • 3
  • 28
  • 37

5 Answers5

3

Yes. It may create larger executable and that is because the member functions which are defined in the class itself, are inline by default, whether you mention the keyword inline in the defintion or not. Usually, inline function causes larger executable because the compiler will define it multiple times wherever it is called from.

Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • There’s also the issue of having different compilation units link dynamically so this can *drastically* reduce the executable itself. – Konrad Rudolph Apr 14 '12 at 17:48
2

.h vs .hpp is the 90% equivalence of

#include <cmath> vs  #include <math.h>

Some people prefer to use .hpp when they are doing exclusive C++ programming. You will see .hpp in libraries like Boost.

However, the other 10% is really important. For example, taking from Boost library doc, they explain the reason of using .hpp over .h:

Most Boost libraries are header-only: they consist entirely of header files containing templates and inline functions, and require no separately-compiled library binaries or special treatment when linking.

If you fall in that case, you should use .hpp, but this can cost longer compilation time. Otherwise, you might want to keep .h style. That's just my personal taste. It isn't C-oriented at all, in my honest opinion.

Further reading:

Splitting templated C++ classes into .hpp/.cpp files--is it possible?

Condensing Declaration and Implementation into an HPP file

C++ templates declare in .h, define in .hpp

Community
  • 1
  • 1
CppLearner
  • 16,273
  • 32
  • 108
  • 163
0

There's all you need here: Header files, pros and cons of putting all you code in them. Hope it helps!

Using header files results in quicker compile time and smaller executable. It also looks considerably cleaner because you can get a quick overview of your class by looking at its .h declaration.

Community
  • 1
  • 1
Erwald
  • 2,118
  • 2
  • 14
  • 20
  • offsite links are not good answers. You need to answer the question here, not just provide a link – David Heffernan Apr 14 '12 at 17:45
  • @DavidHeffernan: The link points to stackoverflow.com how is that off-site? (I agree with the gist of your statement, though) – bitmask Apr 14 '12 at 17:46
  • Also doesn’t really answer the question directly. – Konrad Rudolph Apr 14 '12 at 17:47
  • I totally agree with you David, doesn't seem innapropriate to re-answer / copy paste and answer which is already answered here and which has very relevant explenations to it? – Erwald Apr 14 '12 at 17:49
  • Ah, didn't realise link was to so page. But if that's really the case then marking as dupe is appropriate. – David Heffernan Apr 14 '12 at 17:52
  • :) Just to make sure I edited with the appropriate part of the secondary post which is related to the answer. – Erwald Apr 14 '12 at 17:54
  • This helps, thanks. But I'm not gonna put ALL the code into headers and leaves only one .cpp with "#include"s and and a "main()", because it take longer recompiling when you makes minor change. – RnMss Apr 14 '12 at 18:07
0

You have nothing to worry about. It makes absolutely no difference how it's broken up, it's what your files describe that makes it bigger, not how that description is spread out.

.h or .hpp makes no difference as well.

Mahmoud Al-Qudsi
  • 28,357
  • 12
  • 85
  • 125
  • 1
    Probably the only difference is that text editors will highlight `.h` as C whereas they will highlight `.hpp` as C++. And see Nawaz's answer for reasons why the placement of the definition does matter. – Seth Carnegie Apr 14 '12 at 17:49
  • Very questionable if you're compiling for release/with optimizations. – Mahmoud Al-Qudsi Apr 14 '12 at 18:02
0

To answer your question about a larger executable, yes it will make your executable larger. When a you #include a header file in a source or header file, the preprocessor replaces the #include with the contents of the header file. This is why it is necessary to protect your header files with the following header protection:

#ifndef HDR_H
#define HDR_H

...

#endif

However, you will get linker errors if you include the header file (that has function definitions) in multiple files that are part of the same executable. It would wise for you to split class and function definitions and declarations into .cpp and .hpp files, respectively. This will greatly reduce the amount of linker headaches.

Also, .h = .hpp. Doesn't matter which one you choose. Personal preference...

anthonylawson
  • 761
  • 9
  • 24