I created a sample minimal directory structure with which I am able to re-create my linker error problem. Below is the directory structure I am having:
satyam@sat:~/cpp/build$ ls -R
.:
src template
./src:
main.cxx main.o
./template:
tmp.cxx tmp.hxx tmp.o
Content of each of the file is as below:
tmp.hxx
template <typename T>
class Array {
private:
T *ptr;
int size;
public:
Array(T arr[], int s);
void print();
};
tmp.cxx
#include <iostream>
#include "tmp.hxx"
using namespace std;
template <typename T>
Array<T>::Array(T arr[], int s) {
ptr = new T[s];
size = s;
for(int i = 0; i < size; i++)
ptr[i] = arr[i];
}
template <typename T>
void Array<T>::print() {
for (int i = 0; i < size; i++)
cout<<" "<<*(ptr + i);
cout<<endl;
}
main.cxx
#include <tmp.hxx>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
Array<int> a(arr, 5);
a.print();
return 0;
}
First I moved to template directory and made object file of tmp.cxx with below command:
satyam@sat:~/cpp/build/template$ g++ -c tmp.cxx
Then I moved to src directory and made object file of main.cxx with below command:
satyam@sat:~/cpp/build/src$ g++ -c -I ../template/ main.cxx
Finally I tried to make binary in /cpp/build directory with below command and got this linker error:
satyam@sat:~/cpp/build$ g++ -o prog template/tmp.o src/main.o
src/main.o: In function `main':
main.cxx:(.text+0x4e): undefined reference to `Array<int>::Array(int*, int)'
main.cxx:(.text+0x5a): undefined reference to `Array<int>::print()'
collect2: error: ld returned 1 exit status
I tried looking on other threads of SO and found that including tmp.cxx file in main.cxx will resolve the problem, but I really don't want to go with that option as it doesn't looks right. Not sure where to look further.
Any help regarding this would be great.