从多个文件访问一个文件的变量而无需复制

Access variable from one file from multiple files without copy

我正在尝试在头文件 Declarations.h 中声明一个变量,该变量将被多个文件访问...就像在 C# 中一样,它是一个 public static uint variableXXX 然后您可以从任何地方访问它。

Declarations.h:

#pragma once
#ifndef CLIENT_CONST_H
#define CLIENT_CONST_H

static DWORD LocalPlayerPointer, LocalPlayerAddress, battle_list = 0;

所以变量 LocalPlayerPointer, LocalPlayerAddress 是我希望可以从任何地方访问的变量。

dllmain.cpp:

#include "Includes.h"

LocalPlayerPointer = (DWORD)((moduleBase + dwLocalPlayer));
LocalPlayerAddress = *(DWORD*)LocalPlayerPointer;
if (LocalPlayerAddress == 0) return;

Includes.h:

#include <Windows.h>
#include <iostream>
#include <cassert>
#include <vector>
#include <list>
#include <string>
#include <cstdint>
#include "Utils.h"
#include "Hook/Minhook/include/MinHook.h"
#include "ImGui/imgui.h"
#include "ImGui/imgui_impl_opengl3.h"
#include "ImGui/imgui_impl_win32.h"
#include "functions/functions.h"
#include "../Declarations.h"
#include "../Constants.h"
#include "../Offsets.h"
#include <chrono>
#include <sstream>
#include "../hooks.h"
#include "../targeting.h"

#pragma region OpenGL
#ifdef _WIN64
#define GWL_WNDPROC GWLP_WNDPROC
#endif

#define GLEW_STATIC
#if defined _M_X64
#include "Hook/GLx64/glew.h"
#pragma comment(lib, "Source/Hook/GLx64/glew32s.lib")
#elif defined _M_IX86
#include "Hook/GLx86/glew.h"
#pragma comment(lib, "Source/Hook/GLx86/glew32s.lib")
#endif
#include <gl/gl.h> 
#pragma comment(lib,"opengl32.lib")
#pragma endregion
extern LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
typedef BOOL(__stdcall* twglSwapBuffers) (_In_ HDC hDc);
typedef LRESULT(CALLBACK* WNDPROC)(HWND, UINT, WPARAM, LPARAM);

所以,到这里为止一切都很好。从 dllmain.cpp 我可以访问变量 LocalPlayerPointer, LocalPlayerAddress, battle_list 并且如果我写入它们,值会正确反映。

问题是当我尝试 read/write 来自另一个文件的那些变量时。

让我们以一个名为 targeting.cpp.

的新文件为例

targeting.h

#pragma once
#include "Source/Includes.h"

void Targeting();

targeting.cpp

#include "Source/Includes.h"

void Targeting(){
    std::cout << LocalPlayerPointer << std::endl;
    std::cout << LocalPlayerAddress << std::endl;
}

从 targeting.cpp 开始,std::cout 显示 0。

像 public 静态变量一样访问该变量我做错了什么?

我想访问同一个变量与从哪个文件访问它无关。

编辑:

static DWORD LocalPlayerPointer, LocalPlayerAddress, battle_list = 0; 更改为 extern DWORD LocalPlayerPointer, LocalPlayerAddress, battle_list = 0; 后,出现以下错误:

Error   LNK2001 unresolved external symbol "unsigned long LocalPlayerPointer" (?LocalPlayerPointer@@3KA)    Tibia OpenGL Imgui  C:\Users\Adrian\Documents\Tibia OpenGL Imgui\OpenGLTemplate\dllmain.obj 1   

Error   LNK2005 "unsigned long battle_list" (?battle_list@@3KA) already defined in hooks.obj    Tibia OpenGL Imgui  C:\Users\Adrian\Documents\Tibia OpenGL Imgui\OpenGLTemplate\dllmain.obj 1   

这是我的hooks.h:

#pragma once
#include "Source/Includes.h"

void InitHooks();
extern DWORD BattleHookGateway;
extern DWORD __fastcall BattleHook(DWORD* pThis, DWORD battleList, DWORD localplayer);

我的hooks.cpp:

#include "Source/Includes.h"

DWORD BattleHookGateway;

#pragma region InitHooks
void InitHooks() {
    // get battle list hook
    BattleHookGateway = (DWORD)TrampHook32((char*)BattleListHook, (char*)BattleHook, 5);
}
#pragma endregion

#pragma region Target
DWORD __fastcall BattleHook(DWORD* pThis, DWORD battleList, DWORD localplayer) {
    WriteLine("Hooked :D");
    DWORD battle_list = *(DWORD*)battleList;
    Targeting(battle_list);    
    return ((_BattleListHook)BattleHookGateway)(battleList, localplayer);
}
#pragma endregion

编辑 2:

我现在的问题是我试图用 = 0;

声明它们

因此在删除头文件中的初始化和另一个 cpp 文件中的初始化后它起作用了。

在头文件中放

  extern DWORD battle_int;

在一个 C++ 文件中放入定义

  DWORD battle_int = 0;

不要把 'static' 那是具体的意思 'private to this file'