在主循环外部多个文件上定义全局变量

Defining global variables over multiple files outside main loop

本文关键字:文件 定义 全局变量 循环 外部      更新时间:2023-10-16

我知道这不是很好,但是我需要程序中多个文件的全局变量。这些是我的图形窗口的变量:

  • 名称
  • 大小
  • 状态

我知道我可以制作一个.h文件并声明所有变量:

#pragma once
extern std::string GameName;
extern sf::RenderWindow Window;
extern std::string Status;

然后,我想在main.cpp中定义我的变量,以便所有文件都可以访问这些值。但是除非它们在int main()循环中,否则我无法定义这些。有其他方法,所以我可以定义这些变量,而不是主循环?

编辑

使用Visual Studio 2017。错误:

lnk2001未解决的外部符号"类SF :: RenderWindow窗口" (?window @@ 3vrenderwindow@sf @@ a(立方体 库C: Users George Documents C Files Libraries Cubes Library Cubes Library Cubes Library.OBJ 1

lnk2001未解决的外部符号"类 std :: basic_string,班级 STD ::分配器>状态" (?status @@ 3v?$ basic_string@du?$ char_traits@d@std @@ v?$ selcator@d@d@d@2 @@ std @@ a(cubes 库C: Users George Documents C Files Libraries Cubes Library Cubes Library Cubes Library.OBJ 1

lnk1120 2未解决的外部立方体 库C: Users George Documents C Files Libraries Cubes 库 debug cubes library.dll 1

您可以在main.cpp文件中声明它们,但是要使它们在全球范围内访问,您必须在主函数/循环之外定义它们。如果您将它们在主函数/循环中排列,则它们是本地变量,并且无法(轻松(在全球范围内访问。这样做的方式与您建议的标头文件相结合。

// Global variables...
std::string GameName;
sf::RenderWindow Window;
std::string Status;
int main()
{
   return 0;
}

您也可以将它们放入另一个文件中,例如Globals.cpp。

您会这样做...

文件:main.h

#ifndef __MAIN_H__
#define __MAIN_H__
#include <string>
extern std::string GameName; 
//extern sf::RenderWindow Window;
extern std::string Status;
#endif

文件:main.cpp

#include "stdafx.h"
#include <iostream>
#include "Main.h"
std::string GameName;
//sf::RenderWindow Window; 
std::string Status;
extern void foo(); // Function Prototype
int main()
{
    GameName = "none";
    Status = "none";
    foo();
    std::cout << GameName << " - " << Status << std::endl;
    std::cout << "(HIT A KEY TO CONTINUE)" << std::endl;
    getchar();
    return 0;
}

文件:其他.cpp

#include "stdafx.h"
#include "Main.h"
void foo()
{
    // Global variables declared in Main.cpp are now accessible here
    GameName = "Horizon New Dawn";
    Status = "Finished";
}

这是使用dll中的globals进行操作的方法。

//文件:dllglobals.h

// This file is used in the DLL project
#ifndef __GLOBALS_H__
#define __GLOBALS_H__
#include <string>
extern "C"
{
    extern __declspec(dllexport) std::string GameName;
    extern __declspec(dllexport) std::string Status;
}
#endif//__GLOBALS_H__

//文件:dllglobals.cpp

#include "stdafx.h"
#include "DLLGlobals.h"
extern "C"
{
    // Define Global Variables (no C++ mangling)
    __declspec(dllexport) std::string GameName = "Dishonored 2";
    __declspec(dllexport) std::string Status = "Not Started";
}

//文件:dll.h

#ifndef __DLL_H__
#define __DLL_H__
// This file is included by code using the DLL project
#include <string>
extern "C"
{
    __declspec(dllimport) std::string GameName;
    __declspec(dllimport) std::string Status;
}
#endif//__DLL_H__

//文件:main.cpp

#include "stdafx.h"
#include <iostream>
#include "Main.h"
#include "<path_to_dll_header>DLL.h"
int main()
{
    std::cout << GameName << ": " << Status << std::endl;
    std::cout << "(HIT A KEY TO CONTINUE)" << std::endl;
    getchar();
    return 0;
}