-1

I have 3 headers DirectX.h,Draws.h,Memory.h d

Every function has a definition in its corresponding .cpp so that is ruled out except for DirectX.h ofc I tried a set of solutions to fix it but without success, like not including stdafx.h in all on the Headers.

A little snippet of the three

Memory.h

        #pragma once
        #include "stdafx.h"
        Class Memory
        {
        foo.. 
        };
        extern Memory *gMemory;

Draws.h

#pragma once
#include "stdafx.h"
Class Drawing
{
foo..
};
extern Drawing *Draw;

DirectX.h

#pragma once
#include "stdafx.h"
struct Direct
{
foo
};
extern Direct *DirectX;

Error

1>dllmain.obj : error LNK2001: unresolved external symbol "class Memory * gMemory" (?gMemory@@3PAVMemory@@A)
1>dllmain.obj : error LNK2001: unresolved external symbol "class Drawing * Draw" (?Draw@@3PAVDrawing@@A)
1>dllmain.obj : error LNK2001: unresolved external symbol "struct Direct * DirectX" (?DirectX@@3PAUDirect@@A)
1>Draws.obj : error LNK2001: unresolved external symbol "struct Direct * DirectX" (?DirectX@@3PAUDirect@@A)

stdafx.h

#include <windows.h>
#include <iostream>
#include <d3d9.h>
#include <d3dx9.h>

#include "Memory.h"
#include "Draws.h"
#include "DirectX.h"

dllmain.cpp The only parts where I'm using the extern are

gMemory->FindPattern(..);
if (!DirectX->Line)
    {
    D3DXCreateLine(pDevice, &DirectX->Line);
    }
    Draw->Circle(foo);
Matthew Mackday
  • 65
  • 1
  • 1
  • 6
  • 1
    Show us `dllmain.cpp`. It seems, that you never defined objects `gMemory`, `Draw` and `DirectX` – ikleschenkov Jul 27 '17 at 14:53
  • 1
    Using `extern` does not create the vars. Are they declared somewhere else? [How to correctly use the extern keyword in C](https://stackoverflow.com/q/496448/669576) – 001 Jul 27 '17 at 14:56
  • No, they are not, so what should I do to use with the extern? Declare them before and the extern the variable? – Matthew Mackday Jul 27 '17 at 15:00

1 Answers1

0

extern keyword means, that object is defined somewhere else (in other compilation unit, in linked static lib), in simple words, it's link to the objects, that are instantiated (created) somewhere else, but can be used in the current compilation unit.

In you dllmain.cpp you should declare variables in global (for example) scope:

// Declaration of pointers.
Drawing* Draw = NULL;
Direct* DirectX = NULL;
Memory* gMemory = NULL;

and in int dllmain() (or whatever main function you have):

// Initialization of pointers with newly created objects:
Draw = new Drawing();
DirectX = new Direct();
gMemory = new Memory();
ikleschenkov
  • 972
  • 5
  • 11