While creating a static library using c++11, it fails during linking I think.
I can create a static library and link to it using the information in How to create a static library with g++? with ordinary c++, but if I try to follow the steps using c++11 features, it fails during the linking.
test.cpp:
#include <iostream>
#include <vector>
#include "libtestlib.h"
using namespace std;
int main() {
itest n1={{1,2,3,4},
{5,6,7,8},
{9,0,1,2}};
cout << "testing...\n";
test_print(n1);
return 0;
}
libtestlib.h:
#ifndef testlib
#define testlib
#include <vector>
using namespace std;
typedef vector<vector<double>> dtest;
typedef vector<vector<int>> itest;
template <typename testtype>
void test_print(testtype);
#endif
libtestlib.cpp:
#include <iostream>
#include "libtestlib.h"
using namespace std;
template <typename testtype>
void test_print(testtype &t)
{
int m=t.size();
int n=t[0].size();
for(int i=0; i<m; i++) {
for(int j=0; j<n; j++)
cout << t[i][j] << " ";
cout << endl;
}
cout << endl;
}
this is the output I'm getting:
$ g++ -std=c++11 -c libtestlib.cpp
$ ar rvs libtestlib.a libtestlib.o
r - libtestlib.o
$ g++ -std=c++11 test.cpp libtestlib.a
/tmp/cccJ7SXZ.o:test.cpp:(.text+0x1af): undefined reference to `void test_print<std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > > >(std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >)'
/tmp/cccJ7SXZ.o:test.cpp:(.text+0x1af): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `void test_print<std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > > >(std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >)'
collect2: error: ld returned 1 exit status