Incompatible dll binding and static dllimport data item definition not allowed

Assuming I have these two files:

header.h

class DLL ExportClass{
public:
  ExportClass();
  static int test;
};

Source.cpp

#ifdef EXPORT
    #define DLL __declspec(dllexport)
#else
    #define DLL __declspec(dllimport)
#endif

#include "Header.h"

int ExportClass::test = 0;
ExportClass::ExportClass(){
}

And I will not determine EXPORT(for importing an already exported class with a member static) why I get these warnings:

1>source.cpp(11): warning C4273: 'test' : inconsistent dll linkage
1>          header.h(4) : see previous definition of 'public: static int ExportClass::test'
1>source.cpp(13): warning C4273: 'ExportClass::ExportClass' : inconsistent dll linkage
1>          header.h(3) : see previous definition of '{ctor}'

And this error:

1>source.cpp(11): error C2491: 'ExportClass::test' : definition of dllimport static data member not allowed

If I determine EXPORT, it works. I kind of understand the warnings, but I thought that the static variable and ctor could be ignored by the compiler, because the whole class is declared as __declspec(dllimport)anyway. I want to use the same code base for __declspec(dllexport)and __declspec(dllimport), but it looks like the stll compiler is trying to identify these characters, marked as __declspec(dllexport)in their declaration. What is common practice to solve this problem?

+3
2

, . __declspec (dllimport) , , , . , , , .

C4273 , , , . , , - DLL. , . C4273 - 1, " ". , , . , , , , , DLL. , .

. . , - , , , , . , . , C2491 .

, , , , , .

+16

- :

  • DLL Win32, Project1
  • ,
  • DLL LIB
  • , EXPORT
  • ( /)

4 5 , :

Win32, Project2

:

#include "Project1.h"

#pragma comment(lib, "Project1.lib")

int _tmain(int argc, _TCHAR* argv[])
{
    ExportClass pClass;
    return 0;
}

, , DLL LIB, .

, , DLL/LIB , ?

+3

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


All Articles