-Edit- the 'dupe' has nothing to do with the linker which is the problem here
I compiled the code below with g++ main.cpp test.cpp
If I use test.cpp (so the source is inlined) instead of test.h it'll work fine, but in my real case I need a different file. The linker complains with the below. How do I write this so the linker is happy?
/usr/bin/ld: /tmp/ccgBHBw2.o: in function `TLS wrapper function for Test<4, 8>::v':
main.cpp:(.text._ZTWN4TestILi4ELi8EE1vE[_ZTWN4TestILi4ELi8EE1vE]+0x15): undefined reference to `Test<4, 8>::v'
collect2: error: ld returned 1 exit status
main.cpp
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include "test.h"
//*/#include "test.cpp"
int main(int argc, char *argv[])
{
Test<4, 8> t;
t.test(4, 8);
return 0;
}
test.h
#include<cassert>
template<int A, int B>
struct Test {
static thread_local int v;
static int test(int a, int b) {
assert(A == a);
assert(B == b);
return v;
}
};
test.cpp
#include "test.h"
template<int A, int B>
thread_local int Test<A, B>::v;