【发布时间】:2026-01-31 00:05:01
【问题描述】:
作为我之前在 * 中的问题的延续: Getting LINK error : Extern in C++. How to access the value of a variable which is modified in File A.CPP in another file File B.CPP 在我的 C++ 代码中,我想在文件“B”中使用变量“VarX”,该变量实际上在另一个文件“A”中被修改。 所以我看了一下@以下链接并使用了外部概念。
How do I use extern to share variables between source files?
错误 LNK2001:无法解析的外部符号“unsigned int VarX" (?VarX@@3IA)
我的场景如下:
File1.h
extern unsigned int VarX;
File2.cpp
#include File1.h
VarX = 101;
File3.cpp
#include File1.h
unsigned int temp = VarX;
IMP 注意:在头文件 File1.h 中,除了 Extern 定义之外,还有许多其他结构定义和许多其他定义。
有人可以帮助我吗?我该如何读取在另一个文件 File3.cpp 中的 File2.cpp 中修改的 VarX 的值。
【问题讨论】:
-
VarX = 101;在 C++ 中作为声明或初始化无效。那是你的实际代码吗?
标签: c++ visual-c++ linker linker-errors extern