我有一個頭文件global.h
其中我聲明瞭一些我打算在其他文件中使用的變量。C++全局變量
#ifndef GLOBAL_H_
#define GLOBAL_H_
#include <stdio.h>
typedef struct tag_KG_Data
{
int nKGStationID;
int nKGComPort;
}GLOBAL_VAR;
GLOBAL_VAR g_GlobalVar;
BOOL b_newDataReady;
BOOL b_startedSocketClient;
#endif
起初我只聲明GLOBAL_VAR g_GlobalVar
文件test1.cpp
與extern GLOBAL_VAR g_GlobalVar;
,並且工作得很好。然後我宣佈了2 BOOL
s,並在test2.cpp
中使用它們,但是我得到了error LNK2005: "struct tag_KG_Data g_GlobalVar" ([email protected]@[email protected]@A) already defined in test1.obj
並且對於每個全局變量,我都得到了類似的錯誤。問題是我沒有在test2.cpp
中使用GLOBAL_VAR g_GlobalVar
或test1.cpp
中的任何BOOL
。
所以你有一些工作,現在你已經改變了它,它不再起作用。那裏有一個**大**的線索。 –
@PeteBecker我同意,但我想明白爲什麼不工作,不只是爲了解決問題。 – MRM
+1 @PeteBecker。不能簡單得多。 – WhozCraig