c++ - 如何在多个cpp文件中使用全局变量?
问题描述
常见的.h
extern char *gCert[] = {"Abdul", "Gotez", "Arhaim", "Erhan"};
模板.h
...
模板.cpp
#include "common.h"
char *gCert[];
void generateTemplate()
{
for (int i = 0; i < 4; ++i)
{
/* code */
printf("gCert[i]\n");
}
}
主文件
...
主文件
#include "common.h"
char *gCert[];
void printRandomCert()
{
srand(GetTickCount());
int nSeed = rand() % 4;
for (int i = 0; i < 4; ++i)
{
/* code */
printf("gCert[(nSeed + i) % 4]\n");
}
}
如您所见,我想gCert
在多个 cpp 文件中使用,但不能这样使用。我在下面遇到错误
error LNK2005: "char * * gCert" (?gCert@@3PAPEADA) already defined in template.obj
像上面那样使用全局变量是不可能的吗?如果可能,请分享您的答案。
环境:Windows 10 x64、VisualStudio 2017 C++ Win32 应用程序和 DLL
解决方案
推荐阅读
- python - 如何使用 PyQt 在 QGIS 中以编程方式创建分类符号系统
- python - 如何在字典理解中填充可选字段?
- python - 如何在 Python 中分析 response.text?
- actionscript-3 - 进入下一帧/错误时如何停止循环声音
- databricks - 如何生成有效期超过 48 小时的 databricks 特权令牌
- acl - 您如何使用 adonis-acl 处理资源所有权
- python - 组合两个多维数组
- angular - Angular 中的共享延迟加载组件
- javascript - Javascript:未捕获的类型错误:无法读取未定义的属性“addEventListener”
- r - 如何累积readr::read_lines_chunked的结果?