-1

We have this header file: headerA.h

#pragma once
#include <iostream>

void HeaderADefinedFunction()
{
std::could << "HeaderDefinedFunction called!\n";
}

Then inside sourceB.cpp

#include "headerA.h"
void FunctionB()
{
HeaderADefinedFunction();
}

And inside sourceC.cpp

#include "headerA.h"
void FunctionC()
{
HeaderADefinedFunction();
}

What are the negative aspects of defining the function HeaderADefinedFunction() in the header file itself. Would that definition be in an optimal form for link-time symbol resolution of that particular function?

Employed Russian
  • 199,314
  • 34
  • 295
  • 362
Physician
  • 483
  • 2
  • 7
  • 1
    You are interested in learning about the [one definition rule](https://stackoverflow.com/questions/4192170/what-exactly-is-one-definition-rule-in-c), and ways to write functions that do not violate the one definition rule. – Drew Dormann Dec 18 '21 at 17:59
  • 2
    There would be two definitions of the same function, so ODR violation; no diagnostic required. And, from a project management perspective, if you change the implementation of `HeaderADefinedFunction` you have to recompile two source files; if it was in its own source file you'd only have to recompile that one. – Pete Becker Dec 18 '21 at 18:01
  • @PeteBecker my bad I was typing of the top of my head in the bathroom, it is iostream – Physician Dec 18 '21 at 18:08
  • @Physician -- TMI. – Pete Becker Dec 18 '21 at 18:12
  • Yes. Headers get typically included in multiple places, so you want to limit them to declarations and inline functions, lest you get multiple definition errors during linking. – Petr Skocik Dec 18 '21 at 18:13
  • 2
    Change `void HeaderADefinedFunction()` to `inline void HeaderADefinedFunction()`. The `inline` keyword allows you to define the function in multiple translation units and link them together. – Eljay Dec 18 '21 at 18:24

1 Answers1

3

The code as-shown should produce a link failure with multiply-defined HeaderADefinedFunction() symbol.

The make the code actually valid, the function must be made inline.

Is defining functions in header files malpractice?

Not at all. In fact, template functions must be defined in the header1.

What are the negative aspects of defining the function HeaderADefinedFunction() in the header file itself.

One negative is that if you change the definition, you must rebuild every .cpp which #includes this header, increasing rebuild times.

Another is that your object files are larger.

Would that definition be in an optimal form for link-time symbol resolution of that particular function?

IF you define the function in the header (and make it inline), the compiler may choose to inline it into some (or all) of the call sites. No LTO required.

If you don't, the compiler will not be able to perform such inlining, unless you use LTO.


1 Unless you know all types the template is instantiated with and provide explicit instantiation for all of them.

Employed Russian
  • 199,314
  • 34
  • 295
  • 362