0

I have a C++ project with 5 files:

sde.cpp (containing main()), sde_library.cpp, sde_library.hpp, num_vector.cpp and num_vector.hpp. I'm trying to compile them together with the command

g++ sde.cpp sde_library.cpp num_vector.cpp -o sde.out

The main function contains the two lines

int test = 0;
vector<vector<double>> avg_trace = RK_all(num_traces, many_traces, t_interval, y0, dt, test);

And this function is declared in sde_library.hpp as

template<typename eq_params> std::vector<std::vector<double>> RK_all(unsigned int num_traces, 
bool many_traces, std::vector<double> t_interval, std::vector<double> y0, double dt, eq_params args);

The implementation is found on sde_library.cpp. However, when compiling I get the following linker error message:

/tmp/ccZkiXkj.o: In function `main': 
sde.cpp:(.text.startup+0xd5): undefined reference to `std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > > RK_all<int>(unsigned int, bool, std::vector<double, std::allocator<double> >, std::vector<double, std::allocator<double> >, double, int)' 
collect2: error: ld returned 1 exit status

I was assuming that the appearance of this error was due to me introducing templates to the function RK_all (before I introduced that it was running correctly); I've seen that "undefined references" appear with templates and it's the first time that I'm using them. However, in the main I'm explicitly stating that eq_params will be of the int type, so the compiler should understand what this template is. Is that correct? What am I missing, or what else could it be? And how would I fix that?

nabla
  • 235
  • 2
  • 11
  • 2
    Where is RK_all defined? – ChrisMM Oct 08 '19 at 14:07
  • @ChrisMM I'll update the question to clarify this. – nabla Oct 08 '19 at 14:08
  • From your edit, it is in the cpp file. See the link from @Yksisarvinen as to why this cannot be done. – ChrisMM Oct 08 '19 at 14:10
  • @Yksisarvinen is that still valid? That question is from before C++11 – nabla Oct 08 '19 at 14:10
  • Same limitation applies even in C++20, iirc. – ChrisMM Oct 08 '19 at 14:11
  • So any function which includes templates should be in a header file (not only this one)? – nabla Oct 08 '19 at 14:12
  • 1
    AFAIK, this requirement never changed, from C++98 it's still the same. Not sure about the possibility to explicitly name instantiations, but I suppose it's also C++98. – Yksisarvinen Oct 08 '19 at 14:12
  • Correct; any template functions or classes should be defined in the header file. You can get around this by explicit instantiation in the source file, but it's not the best solution. – ChrisMM Oct 08 '19 at 14:13
  • 2
    If you want to keep your templates truly accept any type, then yes, it has to be in header (any template). If you can or want to limit it to couple of types (e.g. only integral types), you can explicitly name them (and put definiton in .cpp file) - this is also explained in the link. – Yksisarvinen Oct 08 '19 at 14:14

0 Answers0