として宣言されたグローバル変数には、static内部リンケージがあります。これは、各翻訳単位 (つまり.cppファイル) がその変数のプライベート コピーを取得することを意味します。
1 つの翻訳単位のプライベート コピーに加えられた変更は、異なる翻訳単位が保持する同じ変数のプライベート コピーには影響しません。
1 つのグローバル変数を共有する場合は、 1 つの翻訳単位で1 つの定義を提供し、キーワードを指定する宣言を通じて他のすべての翻訳単位がそれを参照できるようにします。extern
test.h
extern int Delay;
void UpdateDelay();
test.cpp
#include "test.h"
void UpdateDelay(){
Delay = 500;
}
main.cpp
#include "test.h"
int Delay = 0; // Do not declare this as static, or you will give
// internal linkage to this variable. That means it
// won't be visible from other translation units.
int main(){
UpdateDelay();
std::cout << Delay << std::endl;
return 0;
}