error:LNK2005终结篇

本文介绍了在C++编程中遇到的LNK2005错误,并提供了详细的解决方案,包括如何正确声明和定义全局变量,避免重复定义的问题。

最近我在写程序时遇到一个这样的问题:

/**
--------------------Configuration: RacingCar - Win32 Debug--------------------
Compiling...
Game.cpp
Linking...
Main.obj : error LNK2005: "struct HINSTANCE__ * g_h_hInStance" (?g_h_hInStance@@3PAUHINSTANCE__@@A) already defined in Game.obj
Main.obj : error LNK2005: "struct HWND__ * g_h_hWnd" (?g_h_hWnd@@3PAUHWND__@@A) already defined in Game.obj
Main.obj : error LNK2005: "int g_draw_picture" (?g_draw_picture@@3HA) already defined in Game.obj
Main.obj : error LNK2005: "int g_gm_situation" (?g_gm_situation@@3HA) already defined in Game.obj
Main.obj : error LNK2005: "struct PARAMMESSAGE g_sPM" (?g_sPM@@3UPARAMMESSAGE@@A) already defined in Game.obj
Main.obj : error LNK2005: "int * g_KeyBoardState" (?g_KeyBoardState@@3PAHA) already defined in Game.obj
Debug/RacingCar.exe : fatal error LNK1169: one or more multiply defined symbols found
Error executing link.exe.

RacingCar.exe - 7 error(s), 0 warning(s)

我想了很多办法,从头文件中增加extern也试过了,于是我找了一些资料:先说明我的解法(在寻找资料以后)

在Game.h中使用(将所要使用头文件中全局变量全部声明为extern)

extern int g_gm_situation ;//game process
extern int g_draw_picture ;//recording the times that the picture is drawn,
//if coming to the second picture of drawing, there will be a blood supply.
//there u can refresh blood as u need at the cost of time and distance.
extern int g_KeyBoardState[256];//contains keyboard state table
extern PARAMMESSAGE g_sPM;

//message and wParam in the PARAMMESSAGE!
extern HWND g_h_hWnd; //save the window handle
extern HINSTANCE g_h_hInStance ;//save the instance

接着在所对应的Game.cpp中(给予再声明一次,当然也应该在这里初始化)

int g_gm_situation = GAME_RUNNING;//game process
int g_draw_picture = 0;//recording the times that the picture is drawn,
//if coming to the second picture of drawing, there will be a blood supply.
//there u can refresh blood as u need at the cost of time and distance.
int g_KeyBoardState[256];//contains keyboard state table
PARAMMESSAGE g_sPM;

//message and wParam in the PARAMMESSAGE!
HWND g_h_hWnd = NULL; //save the window handle
HINSTANCE g_h_hInStance = NULL;//save the instance

其中参考的资料有:

Joseph .M.Newcomer的原话:

you have made the mistake of declaring variables in the header file,consequently, each module which includes it gets a new,conflicting definition of the variable,the first module the linker sees declares the variable, all subsequent attemps to declare it generare the error.

u see,

declare the variable once, in the cpp file associated with .h file,declare it as extern in the header file

Scott McPhillips的原话:

do not define variables in a header file, that will cause them to be defined each tie the header file if #include,define them in one cpp file, then declare them as extern in the header

//.h

extern int* array;//declaration

//.cpp

int* array;//define one place only.

当然还有其它情况产生LNK2005的,这里只提供一个相关连接:

http://news-reader.org/show_filtered.php?group=microsoft.public.vc.mfc&post_nr=465452&pattern=LNK2005&search_header=subject

在这里对LNK2005产生的一些问题都 有相关详细的介绍.请享用LNK2005这顿大餐吧.:)

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值