Linker error with static template member

  • Thread starter Thread starter Stephan Tobies
  • Start date Start date
S

Stephan Tobies

Guest
I have encountered a DLL/Linker problem for which I have not been able to find a solution.

Imagine I have a DLL a.dll with the following header:

// a.h

#pragma once

#ifdef BUILDDLL
#define DLLEXPORT __declspec(dllexport)
#else
#define DLLEXPORT __declspec(dllimport)
#endif

template <typename T>
class A {
public:
static int ID;
};

class DLLEXPORT B : public A<int> { };

By my understanding, this implicitly marks the A<int> instance as dllexport.

Additionally, there is a file that defines the contents of a.dll:

// a.cpp

#define BUILDDLL

#include "A.h"

template <typename T>
int A<T>::ID = 0;

template class DLLEXPORT A<int>;



When I check the exports of the generated a.dll, I find that A<int>::ID is part of the exports:

5 4 0001C160 ?ID@?$A@H@@2HA = ?ID@?$A@H@@2HA (public: static int A<int>::ID)

Now for the use part:


// main.cpp

#include "A.h"

int main()
{
std::cout << B::ID;
}




Now when I try and use a.h and link with a.dll, I nevertheless get a linker error:

2>DllClient.obj : error LNK2001: unresolved external symbol "public: static int A<int>::ID" (?ID@?$A@H@@2HA)

Notice that the symbol is identical, but the linker complains regardless.

Any help or pointer would be greatly appreciated!

Thanks!

Continue reading...
 
Back
Top