C++ Header and Source Directories

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.

Answers


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.


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.


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.


Need Your Help

Random vs SecureRandom in a game

java random

I've made a version of the old risk board game. I'm using java.util.Random to implement the die rolls when armies attack one another. For example, when 3 armies attack a territory with two armies, ...

Convert Crystal Reports to FastReport for Delphi XE4

delphi crystal-reports converter delphi-xe4 fastreport

My company just switched from using Delphi 2007 to Delphi XE4, and now we want to move from Crystal Reports to FastReport. However, we have over 200 reports that need to be converted. I know there ...