Search code examples
cvisual-c++dlldllimportdllexport

Can I import a global variable from a DLL ? Can I do this with a DEF file?


gcc has no problem with this, but I struggle to achieve the same thing with link.exe (visualc)

in dll.c, I define

int myint = 0 ;
int myfunc ( .... ) {  ...  } ;

in dll.h

extern int myint ;
int myfunc ( .... ) ;

in dll.def

LIBRARY mydll
EXPORTS
    myint
    myfunc

Everything works fine, the dll is created, I can link with it, my executable calls myfunc() successfully. But I don't know how to tell the linker that myint is a variable and not a function. So when I try

myint = 1 ;

the application crashes.


Solution

  • If I remember well, with c++ you need to explicitly import global variable (it's implicit with function). I'd try with __declspec( dllimport ) int myint;

    If you want to compile for windows and you are already developing under linux with GCC, I'd cross-compile from linux with mingw, it's a lot easier in my opinion.