在多个文件中共享全局变量作用域 C++
sharing global variable scope in multiple files C++
我有 2 个文件。在一个中声明的全局变量在第二个中不可见。
如果我把它全部放在 一个文件中,那么它就可以工作 ...但是我有 2 个文件。
I would like the output to be "1.0"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
/*-------------------------------------------------------*/
class MsgGateway /* redeclared here like a header file */
{
public:
MsgGateway();
~MsgGateway();
void TestFunc(void);
};
/*-------------------------------------------------------*/
struct settings_t
{
char Ver[4]="1.0";
};
/*-------------------------------------------------------*/
settings_t ESPdata; /* This is the bugger */
MsgGateway* GWClass;
/*-------------------------------------------------------*/
int main(void) /* same as main in cpp */
{
GWClass = new MsgGateway();
GWClass->TestFunc();
return(0);
}
/*-------------------------------------------------------*/
文件ScopeTestMore.cpp
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
struct settings_t
{
char Ver[4]="1.0";
};
class MsgGateway /* initial declaration here in the <header> would usually be */
{
public:
MsgGateway();
~MsgGateway();
void TestFunc(void);
};
void MsgGateway::TestFunc(void)
{
printf("[%s]",ESPdata.Ver);
}
编译器输出 (GCC)
ScopeTestMore.cpp:23:22: error: 'ESPdata' was not declared in this scope
Serial.printf("[%s]",ESPdata.Ver);
exit status 1
'ESPdata' was not declared in this scope
要么
extern settings_t ESPdata
在第二个文件中,或者在两个 cpps 中声明变量 inline 并包含头文件。
在 C++ 中你有声明和定义。
您应该使变量声明在所有 cpp 文件中可见(这通常通过将声明放在所有需要访问全局变量的 cpp 文件中包含的 header 中来完成)。
您应该在一个 cpp 文件中定义变量(该规则有一些例外,但与此上下文无关)。
更多详细信息,例如这里 Variable declaration vs definition。
为什么我们需要声明和定义的背景:How does the compilation/linking process work?
我有 2 个文件。在一个中声明的全局变量在第二个中不可见。
如果我把它全部放在 一个文件中,那么它就可以工作 ...但是我有 2 个文件。
I would like the output to be "1.0"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
/*-------------------------------------------------------*/
class MsgGateway /* redeclared here like a header file */
{
public:
MsgGateway();
~MsgGateway();
void TestFunc(void);
};
/*-------------------------------------------------------*/
struct settings_t
{
char Ver[4]="1.0";
};
/*-------------------------------------------------------*/
settings_t ESPdata; /* This is the bugger */
MsgGateway* GWClass;
/*-------------------------------------------------------*/
int main(void) /* same as main in cpp */
{
GWClass = new MsgGateway();
GWClass->TestFunc();
return(0);
}
/*-------------------------------------------------------*/
文件ScopeTestMore.cpp
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
struct settings_t
{
char Ver[4]="1.0";
};
class MsgGateway /* initial declaration here in the <header> would usually be */
{
public:
MsgGateway();
~MsgGateway();
void TestFunc(void);
};
void MsgGateway::TestFunc(void)
{
printf("[%s]",ESPdata.Ver);
}
编译器输出 (GCC)
ScopeTestMore.cpp:23:22: error: 'ESPdata' was not declared in this scope
Serial.printf("[%s]",ESPdata.Ver);
exit status 1
'ESPdata' was not declared in this scope
要么
extern settings_t ESPdata
在第二个文件中,或者在两个 cpps 中声明变量 inline 并包含头文件。
在 C++ 中你有声明和定义。
您应该使变量声明在所有 cpp 文件中可见(这通常通过将声明放在所有需要访问全局变量的 cpp 文件中包含的 header 中来完成)。
您应该在一个 cpp 文件中定义变量(该规则有一些例外,但与此上下文无关)。
更多详细信息,例如这里 Variable declaration vs definition。 为什么我们需要声明和定义的背景:How does the compilation/linking process work?