0

I have the following files:

listDriverTest.cpp
src/List.cpp
headers/List.h

The include in List.cpp is

#include "../headers/List.h"

The include in listDriverTest.cpp is

#include "headers/List.h"

When I compile with the following statement,

g++ listDriverTest.cpp "src/List.cpp"

I end up with a fair number of 'undefined reference' errors, e.g.

listDriverTest.cpp:(.text+0x81): undefined reference to `List<int>::List()'
listDriverTest.cpp:(.text+0x8f): undefined reference to `List<int>::add(int)'
listDriverTest.cpp:(.text+0x9d): undefined reference to `List<int>::add(int)'
...

How should I properly use includes and compile these three files in order for the compilation to work properly? I have gotten listDriverTest.cpp to compile and run properly with all the files in the same directory, but not when they're broken up like this.

ildjarn
  • 62,044
  • 9
  • 127
  • 211
Tanaki
  • 2,575
  • 6
  • 30
  • 41

3 Answers3

1

See my answer in Must a child of a template class also be a template class?.

It is probably a different question, but the same answer applies.

Community
  • 1
  • 1
d_inevitable
  • 4,381
  • 2
  • 29
  • 48
  • Ah, thank you. I /completely/ forgot about the issue with having a header file and cpp file for template methods that belong to a class. That fixed the problem right up! – Tanaki Apr 16 '12 at 18:02
1

It looks like the object file produced by compiling src/List.cpp already contains the specialization List, but it's in a different directory than the object file of listDriversTest.cpp. Hence, the linker cannot find it.

Of course, this depends on how you've organized your template code.

bjhend
  • 1,538
  • 11
  • 25
0

Your program compiled properly on my machine.
Just remove the double quotes around src/List.cpp I think your problem is something else.

I added a function void list(void) in list.cpp which printed "list".
The same signature was added to list.h.

prathmesh.kallurkar
  • 5,468
  • 8
  • 39
  • 50