繁体   English   中英

在主循环外的多个文件中定义全局变量

[英]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 Studio2017。错误:

LNK2001无法解析的外部符号“ sf :: RenderWindow Window类”(?Window @@ 3VRenderWindow @ sf @@ A)多维数据集库C:\\ Users \\ George \\ Documents \\ C ++ \\ Files \\ Libraries \\ Cubes Library \\ Cubes Library \\ Cubes Library。对象1

LNK2001无法解析的外部符号“ class std :: basic_string,class std :: allocator> Status”(状态@@ 3V?$ basic_string @ DU?$ char_traits @ D @ std @@ V?$ allocator @ D @ 2 @@ std @@ A)多维数据集库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 Library \\ 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;
}

文件:Other.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中使用全局变量的方法。

//文件: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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM