0

I have a regular MFC application from which I want to export a variable and use it in a dll. Here is summary of declarations/definitions in exe/dll which should like here.

__declspec(dllexport) BOOL g_status; // In EXE
__declspec(dllimport) BOOL g_status; // in DLL

The linker error is following which seems to indicate it can't find due to name mangling?

error LNK2001: unresolved external symbol "__declspec(dllimport) int g_status" (__imp_?g_status@@3HA)

Then I tried this combination adding extern "C"' to declaration in dll but it doesn't work because if I use C decorations on dll, the same has to be done on exe side as well but using 'extern "C" on exe side actually forces it to be become declaration!

__declspec(dllexport) BOOL g_status; // in EXE
extern "C" __declspec(dllimport) BOOL g_status; // in DLL

Linker Error below:

error LNK2001: unresolved external symbol __imp__g_status   

I am a bit lost. I see a lot of examples with functions but not many with variables, how to export them and if name decorations are still in play?

zar
  • 11,361
  • 14
  • 96
  • 178
  • Your first "in EXE" shows `dllimport`, should be `dllexport` – franji1 Jul 07 '17 at 19:12
  • @franji1 thanks i verified, that was a typo in post only, fixed it. – zar Jul 07 '17 at 19:14
  • 1
    This almost always ends up as a circular dependency. You can't get the DLL linked until you have linked the EXE. But you can't get the EXE linked until you linked the DLL. It is [formally possible](https://stackoverflow.com/questions/2727020/what-is-use-of-exp-and-what-is-the-difference-between-lib-and-dll), but not directly supported by the IDE. Best avoided of course, if you need another DLL to be the home of that variable then so be it. You'll probably find some more use for it. – Hans Passant Jul 07 '17 at 19:18
  • @HansPassant that seems to be it. I will have to link it differently as documented [here](https://msdn.microsoft.com/en-us/library/kkt2hd12.aspx) – zar Jul 07 '17 at 20:10
  • 1
    This is better handled by having the receiving DLL export a function that the providing EXE (or another DLL) can then call at runtime to pass the desired variable to the DLL. Or have the receiving DLL export a pointer variable that the providing EXE/DLL can then import and assign a memory address to. – Remy Lebeau Jul 07 '17 at 22:49
  • @RemyLebeau I got it working by defining it in the right dll and accessing it in another which uses it but I still get linker error in other dlls. I noticed it's in those which don't have dependency on where I defined it. So does the dependency play into it as well? – zar Jul 10 '17 at 22:01

1 Answers1

0

This indeed was circular dependency issue pointed by Hans Passant in comment. In my case, I wanted this variable for debugging aid temporarily so I didn't want to go through figure it out.

I end up declaring it in one of the dll and used it in the other with simple defintions/declarations as below.

__declspec(dllexport) BOOL g_status; // In EXE
__declspec(dllimport) BOOL g_status; // in DLL

There was one other caveat though, the caller dll must have dependency to the callee otherwise a linker error will still occur. I was getting this linker error:

error LNK2001: unresolved external symbol "__declspec(dllimport) int g_status" (__imp_?g_status@@3HA)

It went away when I set the dll dependencies.

Hope this helps some.

zar
  • 11,361
  • 14
  • 96
  • 178
  • "It went away when I set the dll dependencies." what do you mean by that? Could you please elaborate? Specifically what is "dll dependencies" and how to do it. – Spade 000 Sep 08 '20 at 09:22
  • @Spade000 I don't fully remember but I think what I meant is you need to set dependency in your solutions. The project that is using the exported symbol should be set to be dependent on the one it us using. – zar Sep 08 '20 at 20:00
  • I mean, did you add a parameters to the compiler (gcc or clang) when compiling it? I'm not sure where I can set dependency you mean. – Spade 000 Sep 09 '20 at 12:22
  • @Spade000 no, in visual studio, you just set project dependencies in solution properties. – zar Sep 09 '20 at 17:28
  • dang. I thought you were only using a compiler. Anyway, I'll go look it up how to simulate this things in my CLI. – Spade 000 Sep 10 '20 at 13:52