On May 22, 1:17*am, Leclerc <gordan.sikic.rem...@this.inet.hr> wrote:
> http://www.parashift.com/c++-faq-lite/templates.html
>
> read about linking errors, separating templates into headers-sources,
> and the rest of course
Leclerc,
I've reviewed the Parashift info regarding templates and header / body
separation, and none of the information helps.
To re-iterate, I'm compiling each .cpp file just fine. I believe I've
separated the the .h and .cpp parts correctly.
This guide says nothing about linking templated base / derived
classes.
It's linking that has the problem. This is unexpected, as one of the
input compilation unit's defines the symbols that it complains about
not finding.
willo:~/tmp$ make
g++ -c -o main.o main.cpp
g++ -c -o B.o B.cpp
g++ -c -o A.o A.cpp
g++ main.o B.o A.o -o main
B.o: In function `B<int>::B()':
B.cpp

.text._ZN1BIiEC1Ev[B<int>::B()]+0x11): undefined reference to
`A<int>::A()'
B.o: In function `B<int>::~B()':
B.cpp

.text._ZN1BIiED1Ev[B<int>::~B()]+0x11): undefined reference to
`A<int>::~A()'
collect2: ld returned 1 exit status
make: *** [main] Error 1
willo:~/tmp/$ nm --demangle A.o
0000000000000000 t instantiate_templated_class()
0000000000000000 W A<int>::A()
0000000000000000 W A<int>::~A()
U __gxx_personality_v0
willo:~/tmp/$ nm --demangle B.o
0000000000000000 t instantiate_templated_class()
U A<int>::A()
U A<int>::~A()
0000000000000000 W B<int>::B()
0000000000000000 W B<int>::~B()
U __gxx_personality_v0
What is preventing the symbols for A in A.o from being used at link
time?
I could instantiate the A<int> symbols in B.o, but then I'd have
duplicate symbols, which can cause other linking errors in future
usage.
-- Charles Wilcox