简体   繁体   English

如何在多个.cpp文件中使用全局变量?

[英]How to use global variables in multiple .cpp files?

I have this simple program which tries to print my global variable in a separate file. 我有这个简单的程序,试图在一个单独的文件中打印我的全局变量。 I'm using the Visual Studio 2013 professional IDE. 我正在使用Visual Studio 2013专业 IDE。

print.h print.h

#ifndef PRINT_H_
#define PRINT_H_

void Print();

#endif

print.cpp print.cpp

#include "print.h"

#include <iostream>

void Print()
{
    std::cout << g_x << '\n';
}

source.cpp source.cpp

#include <iostream>
#include <limits>

#include "print.h"

extern int g_x = 5;

int main()
{
    Print();

    std::cin.clear();
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    std::cin.get();

    return 0;
}

I get a compiler error error C2065: 'g_x' : undeclared identifier . 我收到编译器错误error C2065: 'g_x' : undeclared identifier

I've searched through this forum and was unable to find anyone else having my problem. 我搜索了这个论坛,但是找不到其他遇到我问题的人。 I've tried re-declaring my global variable in the separate .cpp file with no success. 我尝试在单独的.cpp文件中重新声明我的全局变量,但没有成功。 As you can see, I've included the necessary header guards and assigned my global variable the extern keyword. 如您所见,我已经包含了必要的标题保护,并为全局变量分配了extern关键字。 This is my first time testing global variables in multiple files. 这是我第一次测试多个文件中的全局变量。 Obviously I'm missing something simple. 显然我缺少一些简单的东西。 What do I need to change or add to make my program work? 我需要更改或添加什么才能使程序正常工作?

EDIT: I found this topic useful in understanding the difference between extern and the definition of a global variable. 编辑:我发现本主题对理解extern和全局变量定义之间的区别很有用。

The compiler is compiling print.cpp . 编译器正在编译print.cpp It knows nothing about source.cpp while it is compiling print.cpp . 在编译print.cpp时,它对source.cpp print.cpp Therefore that g_x that you placed in source.cpp does you absolutely no good when print.cpp is being compiled, that's why you get the error. 因此,在编译print.cpp时,放置在source.cpp中的g_x对您绝对没有好处,这就是为什么会出现错误。

What you probably want to do is 您可能想做的是

1) place extern int g_x; 1)放置extern int g_x; inside of print.h . print.h内部。 Then the compiler will see g_x when compiling print.cpp . 然后,编译器将在编译print.cpp时看到g_x

2) in source.cpp , remove the extern from the declaration of g_x : 2)在source.cpp ,从g_x的声明中删除extern

int g_x = 5;

Move your global declaration to a common header, like common.h : 将您的全局声明移到公共标头,例如common.h

#ifndef COMMON_H_
#define COMMON_H_

extern int g_x;   //tells the compiler that g_x exists somewhere

#endif

print.cpp : print.cpp

#include <iostream>

#include "print.h"
#include "common.h"

void Print()
{
    std::cout << g_x << '\n';
}

source.cpp : source.cpp

#include <iostream>
#include <limits>

#include "print.h"
#include "common.h"

int g_x;

int main()
{
    g_x = 5;   //initialize global var
    Print();

    std::cin.clear();
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    std::cin.get();

    return 0;
}

In other .cpp files, you can access g_x including the common.h header. 在其他.cpp文件中,您可以访问g_x包括common.h标头)。

extern int g_x;

belongs to .h, and you need to add 属于.h,您需要添加

int g_x =5; 

to some of .cpp. 到一些.cpp。

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

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