0

I am trying to export/import a variable between exe and a dll. There are three folders. B.cpp in in one folder which is producing exe. C.cpp is in another folder which is producing a dll.

A.h is in a shared folder, if we need to use something between folders we use this folder. It only has the header files.

A.cpp is in folder which is producing the dll.

In A.h

__declspec(dllexport) extern int* val;

In A.cpp

__declspec(dllexport) int* val;

B.cpp

#include <A.h>
set val

C.cpp

#include <A.h>
get val and using val in some function

**I am getting error B.obj : error LNK2001: unresolved external symbol

I would really appreciate, any help or atleast some advice, where I can learn more about this problem.

mg9893
  • 11
  • 1
  • 5
  • Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Alan Birtles Oct 26 '18 at 06:50
  • @AlanBirtles Can you point out the mistake more precisely, I am not able to figure out by the link you provided – mg9893 Oct 26 '18 at 06:57
  • Not without a [mcve], are you linking to the library file of your dll from your executable? Also note that you probably want `#define` not `#pragma push_macro` – Alan Birtles Oct 26 '18 at 08:26

1 Answers1

0

For your macros, use something like

#ifdef VAL_EXPORTS  
#define VAL_API __declspec(dllexport)   
#else  
#define VAL_API __declspec(dllimport)   
#endif

In A.h, declare it using extern, i.e. VAL_API extern int* val; and in A.cpp, define it without extern, i.e. VAL_API int* val = nullptr;

In B.h, #include A.h and assign whatever value to it.

In C.h, just #include A.h and you can use val.

Nyque
  • 351
  • 3
  • 10
  • Instead of putting it in B.h and C.h, Shouldn't it be put in B.cpp and C.cpp. Also since in B.cpp, I will be exporting, I should also define #define VAL_EXPORTS? – mg9893 Oct 26 '18 at 08:29
  • @mg9893 You can put it in `B.cpp` or `C.cpp` as well. You're not exporting or importing at `B.h/cpp` or `C.h/cpp`. You're only exporting at `A.h` (which I assume is in the dll) so only `A.h` needs `#define VAL_EXPORTS`. – Nyque Oct 26 '18 at 08:52
  • Are we not importing in B.cpp or C.cpp? If we don't define VAL_EXPORTS in B.cpp and C.cpp we are executing the else part which means we are importing – mg9893 Oct 26 '18 at 09:07
  • @mg9893 No, it is not imported in `B.cpp` or `C.cpp`. For an easy test, you can just replace `VAL_API` with `__declspec(dllexport)` and the result will be the same. – Nyque Oct 26 '18 at 09:16
  • I am unable to solve problem using this getting the same error but in B.obj. (error LNK2001: unresolved external symbol) Any idea why this may happen? – mg9893 Oct 26 '18 at 09:28
  • @mg9893 Can you edit your post and update your code? Specify if the file is in dll or exe as well. – Nyque Oct 26 '18 at 09:33
  • @mg9893 Just to confirm, you're using implicit linking and you're setting `val` in `B.cpp` inside a function? Also, your `#include` should be using quotation marks `""` instead angle brackets `<>`. – Nyque Oct 26 '18 at 10:01
  • yes I am using implicit linking and setting val in B.cpp inside a function. And i corrected to "" instead of <>. – mg9893 Oct 26 '18 at 10:15
  • @mg9893 Did you link to `dll_name.lib`? I can only replicate your error when I did not link to it. If you're using Visual Studio, it's under `Properties` -> `Linker` -> `Input` -> `Additional Dependencies`. Make sure you add `dll_name.lib` and not `dll_name.dll`. When you compile a project into a dll, a lib file will be automatically generated as well. – Nyque Oct 26 '18 at 11:04