1

I'm trying to define a header-only library which gets included in multiple object-files. Everything works if I define all the class's methods in-line,

// lib.hpp
class Foo {
  void fn() { ... }
};

But a dependency loop with class Bar requires I define Foo::fn out of line.

// lib.hpp
class Foo {
  void fn();
}
class Bar { ... }
void Foo::fn() { ... }

And I am suddenly getting multiple definitions of Foo::fn (as expected).

My question is how to define the method out-of-line while making the visibility local to each object file (as it was when it was in-line). If this were a function, I would slap a static in front, but that seems to only further incense my compiler.

charmoniumQ
  • 5,214
  • 5
  • 33
  • 51
  • 3
    Function definitions in the header have to be inline. You need to add `inline` in front of them. (You don’t need it when defining them in the class body because they are implicitly inline there) – t.niese Dec 09 '20 at 22:03
  • 1
    Also worth considering "What do I gain from separating the method implementations?" If you aren't placing them in a CPP file where they don't need to be compiled as often, all you're doing is adding extra work for yourself by separating. Is the extra work worth it? – user4581301 Dec 09 '20 at 22:07
  • @t.niese if make your comment into an answer, I'll accept it. – charmoniumQ Dec 09 '20 at 23:16

0 Answers0