好LNK2001

The good ol' LNK2001

本文关键字:LNK2001      更新时间:2023-10-16

>我有 3 个标题DirectX.hDraws.hMemory.hd

每个函数在其相应的.cpp中都有一个定义,因此除了 DirectX.h ofc 之外,该定义被排除在外 我尝试了一组解决方案来修复它,但没有成功,例如在标题上没有includingstdafx.h。

三者的一小部分

内存.h

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

平局.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;

错误

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我使用extern的唯一部分是

gMemory->FindPattern(..);
if (!DirectX->Line)
{
D3DXCreateLine(pDevice, &DirectX->Line);
}
Draw->Circle(foo);

>extern关键字意味着该对象是在其他地方定义的(在其他编译单元中,在链接的静态库中(,简而言之,它是指向对象的链接,这些对象在其他地方实例化(创建(,但可以在当前编译单元中使用。

dllmain.cpp中,您应该在全局(例如(范围内声明变量:

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

int dllmain()(或您拥有的任何主要功能(中:

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