I reproduced the behavior that I experienced in a larger project in the next few lines of code. I refused guards #ifndefand directives #includein an attempt to improve readability. Linker error occurs on invocation make. The makefile is included at the end of the question.
Class C inherits from B, which inherits from A. O is a completely different class.
Linker complains:
g++ -o main main.cpp -L. -lABC -lO
./libO.a(O.o): In function `O::foo(A)':
O.cpp:(.text+0x1f): undefined reference to `C::C(A const&)'
Here is the source code. I tried to make it so small and as readable as possible. Any idea what the problem is?
class A
{
public:
A();
A(const A& a);
};
A::A() {}
A::A(const A& a) {}
class B : public A
{
public:
B(const A& a);
};
class C : public B
{
public:
C(const A& a);
};
B::B(const A& a) : A(a) {}
C::C(const A& a) : B(a) {}
class O
{
public:
void foo(A a);
};
void O::foo(A a)
{
C c(a);
}
The main thing here:
int main()
{
A a;
O o;
o.foo(a);
return 0;
}
And here is the makefile:
%.o: %.cpp %.h
g++ -c $<
.PHONY: all
all: mklibs main
main: main.cpp
g++ -o $@ main.cpp -L. -lABC -lO
mklibs: libABC.a libO.a
libABC.a: A.o BC.o
ar -r $@ $^
libO.a: O.o
ar -r $@ $^
source
share