Search code examples
c++inline

Compilation of C++ member functions defined outside the class body


I have a hpp file like this:

#ifndef __MYLIB_H
#define __MYLIB_H

class A {
public:
  void func1();
  void func2();  
};

void A::func1() {
  // maybe do something
}

#endif

There is a corresponding cpp file which has the implementation of func2. The header file is included by other files in the project (and those are included by yet more files). When I try to build this, I get "multiple definition" linker errors for func1. Why should that happen? Since I've protected the header file with the #ifndef, I did not expect the error.

If I add the keyword inline to the definition of func1, then everything is fine. So if I don't care about a function being inlined and I don't want to have the definition in the class body, I cannot include the definition it in the hpp file? If someone can explain the what's going on here, it'll be very helpful. I'm using GCC 6.


Solution

  • Recall that the content of the entire header, along with any files that it may include, is virtually "copy-pasted" into each translation unit* that includes the header at the point of inclusion.

    That is why the effect of including the header in multiple CPP files is the same as if you literally copy-pasted function definition into each CPP, i.e. it is the same as if you wrote multiple identical definitions of the same member-function.

    The problem is solved by declaring the function inline, because C++ allows multiple definitions of inline functions, as long as they are identical to each other. Moving the body of the function into the declaration of the class also works, because such functions are automatically considered inline.

    * In this context, "translation unit" is a fancy name for your CPP file.