在主循环外部多个文件上定义全局变量
Defining global variables over multiple files outside main loop
我知道这不是很好,但是我需要程序中多个文件的全局变量。这些是我的图形窗口的变量:
- 名称
- 大小
- 状态
。
我知道我可以制作一个.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;
}
- C++无法定义虚拟函数 OUTER 类和头文件
- QML:修改在不同QML文件(而非main.QML)中定义的子对象的属性
- 尝试调用 .h 文件中定义的变量时出现变量未定义错误
- 在头文件和 cpp 文件中使用一次 #pragma 时出现结构重定义错误
- C++编程从外部文本文件定义数组大小
- MAKE:找不到包含的用户定义的头文件?
- 定义C++新的环境变量并在 bat 文件中使用它
- 在头文件中定义变量不会出错
- 即使我没有包含多个文件,C++中的多个定义错误
- 如何检查函数是否在LLVM Instrumentation pass的ModulePass的系统头文件中定义?
- 查找定义我的 C/C++ 函数/宏的文件比'grep'更简单的方法
- 将文件复制到自定义位置,存在字符串转换问题
- 如何正确定义C++类析构函数并将其链接到主文件?
- C2011: 'Card':"类"类型重新定义(尽管使用了包含保护并且没有在文件中重新定义.cpp类)
- 从头文件定义嵌套类
- 使用标头文件定义MT19937和normal_distribution
- 为特定文件定义
- 从.h和.cpp文件定义纯虚拟函数会产生链接器错误吗
- C 编程从外部输入文件定义数组大小
- 编译头文件定义了一个模板类,该类还包括其他头文件