1

Say I have 2 simple classes A and C, also I have global external variables in form of pointers to A and C. Code:

//global.h
#pragma once
#include "A.h"
#include "C.h"
struct A;
struct C;
extern A* external_a;
extern C* external_c;
/////////////////////////////////////////
//A.h
#pragma once
struct A {
    void get();
};
/////////////////////////////////////////    
//A.cpp
#include "A.h"
void A::get() {}
/////////////////////////////////////////
//C.h
#pragma once
# include "global.h"
struct C {
    void doit();
};
/////////////////////////////////////////   
//C.cpp
# include "C.h"
void C::doit() { external_a->get(); }
/////////////////////////////////////////

I listed only DLL code. The point was for DLL user to provide from (declare in) his EXE code say main.cpp global variables for my DLL code to use. So user is not in the same project while I want to use that external variables in multiple files inside my DLL project.

I get two errors from this code regarding unresolved external symbol "struct A * external_a" enter image description here

I have tried to write __declspec(dllexport) in class defenitions and after external keyword. It does not help to fix the error. So I wonder what shall be done to be able to compile such project?

DuckQueen
  • 772
  • 10
  • 62
  • 134
  • What is present inside "A.h". Where have u defined A* external_a, it should be declared first to use with extern keyword. – Sumeet May 31 '16 at 04:46
  • 1
    I don't see where `external_a` and `external_c` are defined. – R Sahu May 31 '16 at 05:10
  • Did you try the obvious `extern __declspec(dllimport) A* external_a;`, and `__declspec(dllexport) A* external_a;` in the DLL? (by "obvious" I mean "obvious if you already know how to export functions) – user253751 May 31 '16 at 05:20
  • Yes I tried - identical errors. external_a and external_c are tobe defined by DLL user. – DuckQueen May 31 '16 at 05:28
  • Possible duplicate of [use static class variable/function across dlls](http://stackoverflow.com/questions/8654327/use-static-class-variable-function-across-dlls) – DuckQueen Jun 01 '16 at 03:27

1 Answers1

2

The error message is clear and it has nothing to do with DLL or DLL import/export. You have declared "external linkage" extern A* external_a;, but external linkage to what?

In one of your *.cpp file (usually the one containing main entry point) there should be a declaration for a global variable. If you are using pointer, then you must also allocate it. For example

File "main.cpp"

#include "a.h"

//Initialize pointer here:
MyStructA* global_a;
int main()
{
    //Allocate data here
    //(unless this is a DLL project and you are passing a variable)
    global_a = new MyStructA;
    foo();
    return 0;
}

(I changed the variable name to global_a which makes more sense)

If you want to use global_a in another file in the same project, use the extern MyStructA* global_a;. For example, in "otherfile.cpp"

#include "a.h"

extern MyStructA* global_a;
foo();
...


If you have EXE project, and a separate DLL project, then use dllexport/dllimport to access functions in a DLL. Declare data in EXE project, then use functions to pass the data. extern is not relevant in this case. For example:

Use dllexport In DLL project:

MyStructA *global_a;

__declspec(dllexport) void foo(MyStructA *p)
{
    global_a = p;
}

Use dllimport in EXE project:

__declspec(dllimport) void foo(MyStructA *p);

MyStructA A;
foo(&A);
Barmak Shemirani
  • 30,904
  • 6
  • 40
  • 77
  • I listed only DLL code. The point was for DLL user to provide from his say main.cpp global variables for my code to use. So user is not in the same project. While I want to use that external variables in multiple files inside my DLL project – DuckQueen May 31 '16 at 16:23
  • I have explained them both. You should figure it out separately. Write a simple test project for `extern`, write another test project for DLL, then put them together if needed. – Barmak Shemirani May 31 '16 at 16:34
  • This is not about having them sepratly. It is about providing instance of global variable into Dll from external EXE, w2hile using that global variable inside exe – DuckQueen May 31 '16 at 17:11