Static class members in a shared library

I have a class like

class K {
  static int a;
  static int b;
}

I would like to create a shared library (dll) containing this class K. In the cpp file compiled in the library, I call

int K::a = 0;
int K::b = 0;

to create static variables. The dll compiles without errors, but when I use the library, I get an unresolved external character error for the K::aand members K::b. In the main program, where I want to use it, I include the same header with the class declaration K, the only difference is that for the library I use class __declspec( dllexport ) K { ... } for the main programclass K { ... }

Perhaps I am making more than one mistake, so my questions will be, how can I

  • tell the linker to share the static member class in the library?
  • , ?

PS. Visual Studio 2008...

+3
2

__declspec( dllimport ).

, . ( ):

#ifdef COMPILE_DLL
#define DLL_SPEC __declspec( dllexport )
#else
#define DLL_SPEC __declspec( dllimport )
#endif

class DLL_SPEC K {
   static int a;
   static int b;
}

cpp :

int K::a = 0;
int K::b = 0;

, COMPILE_DLL, .

+1

libarary , .

DLL, .

0

Source: https://habr.com/ru/post/1710796/


All Articles