1

I was told that the only event in which it's really a good idea to define functions in header files is if the function is marked inline or is a function template. Apparently,or so I was told, one of the key reasons for this is the one definition rule- a rule allowing only only one function definition per translation unit.I was told that you would get one definition of that function for every cpp file that includes that header. I am however having issues understanding how you would get one definition of that function for every cpp file in C++. I'm having a bit of trouble visualizing why that might be the case. So, would someone mind offering some insight into why that's the case? Cheers!

Mrcitrusboots
  • 317
  • 3
  • 14

2 Answers2

3

When you #include a file, the compiler acts as if you'd just copy-pasted the file into the current file.

That is, if you have three files:

// header.h
void foo() {/* do stuff */}

// a.c
#include "header.h"
void a_func() {/* do stuff */}

// b.c
#include "header.h"
void b_func() {/* do stuff */}

then the compiler treats it as if you'd copied the contents of header.h into both a.c and b.c - that is, it behaves exactly as if you had:

// a.c
void foo() {/* do stuff */}
void a_func() {/* do stuff */}

// b.c
void foo() {/* do stuff */}
void b_func() {/* do stuff */}

Clearly, there are multiple definitions of foo here - as there's one in each file.

user253751
  • 57,427
  • 7
  • 48
  • 90
  • The ODR is definitely more advanced, but is outside the scope of the OP's question. It is closely related and is good information for suggesting further learning, but I think this answer is fine. – Jed Schaaf Mar 18 '16 at 04:35
  • @immibis Right, I am familiar with include guards. However, I'm still not really clear on how implementing functions in the header file will create a conflict with the one definition rule, but implementing them in a separate cpp file doesn't. What are the subtleties that make that the case? Thanks for taking the time to help me out! – Mrcitrusboots Mar 18 '16 at 05:20
  • @Mrcitrusboots There's another part of the one definition rule that says a non-inline function may only be defined once in the entire program. If you put the function implementation in a header file, and include that file in several translation units, then the function is defined in several translation units. – user253751 Mar 18 '16 at 09:38
  • @immibis Thank you, that's exactly what I was looking for! – Mrcitrusboots Mar 18 '16 at 22:43
1

For simple function such as sum, definition would be:

double sum(double a, double b) { return a + b; }

and declaration:

double sum(double a, double b);

You place declaration in header, e.g. sum.h, so later you can use sum's definition, e.g. placed in sum.cpp. Later, linker finds the definition of sum in sum.o object file.

Since #include pragma is replaced with headers' contents, if you place your function's definition in header, you'll have multiple identical functions and also linker error.

Community
  • 1
  • 1
yuyoyuppe
  • 1,582
  • 2
  • 20
  • 33