0

I have a Visual Studio C++ project containing main program and a DLL module. The DLL has a class with the following definition:

// .h
#ifdef _USRDLL
    #define DLLAPI __declspec(dllexport)
#else
    #define DLLAPI __declspec(dllimport)
#endif

class DLLAPI EClass
{
public:
    static int value;

    static int get_value();
};

// .cpp
int EClass::value = 1;

int EClass::get_value()
{
    return value;
}

The DLL project is compiled successfully, both symbols (value and get_value) are observable by Dependency Walker.

In the main program, I can call the static function get_value

int v = EClass::get_value();  // Ok, v = 1

but when I try to access the field value directly

int v = EClass::value;  // Error

I get an error

LNK2001 unresolved external symbol "public: static int EClass::value" (?value@EClass@@2HA)

It is possible to avoid using accessors for static fields?

Andrey Nasonov
  • 2,619
  • 12
  • 26
  • Look like you not exporting the static, did you try to add `DLLAPI` before its declaration? (for outside the class) – SHR Sep 26 '15 at 22:54
  • maybe also need to add to the header: `extern DLLAPI int EClass::value;` to tell your app to dllimport it. – SHR Sep 26 '15 at 22:59
  • Yes, I have tried it with no success. The static field is exported correctly: I have an access to it using the accessor method and I see this field in DLL. – Andrey Nasonov Sep 26 '15 at 23:01
  • possible duplicate of [Exporting static data in a DLL](http://stackoverflow.com/questions/2479784/exporting-static-data-in-a-dll) – wimh Sep 26 '15 at 23:09
  • 1
    @AndreyNasonov Are you using your dll from other dll? if so your usage of the `_USRDLL` macro instead of a unique macro for each dll may cause the problem, anyway, for a single dll and a single exe using it, it works for me without any change on my visual studio 10 express. – SHR Sep 26 '15 at 23:17
  • Thank you for the right way. The problem was `_USRDLL` macro defined in both DLL and EXE project configurations. I have no idea how this was happened. Now it's all working fine. – Andrey Nasonov Sep 26 '15 at 23:27

1 Answers1

0

The macro _USRDLL should be defined only in the DLL project.

Andrey Nasonov
  • 2,619
  • 12
  • 26