Linkage error - Variable already defined
-
This problem probably sounds silly but I can't get my head round it; there it goes:
#include <DelimFileParser.h>
int a; //I can't define any type of variable in here
typedef struct
{
std::string m_devTrainId;
std::string m_devIp;//sockaddr_in
...The compiler's message:
Linking...
CDeviceFileParser.obj : error LNK2005: "int a" (?a@@3HA) already defined in CCommsManager.obj
CSnapLogger.obj : error LNK2005: "int a" (?a@@3HA) already defined in CCommsManager.obj
snaplogger_main.obj : error LNK2005: "int a" (?a@@3HA) already defined in CCommsManager.objsnaplogger_main.h includes CSnapLogger.h CSnapLogger.h includes CCommsManager.h CCommsManager.h includes CDeviceFileParser.h
-
This problem probably sounds silly but I can't get my head round it; there it goes:
#include <DelimFileParser.h>
int a; //I can't define any type of variable in here
typedef struct
{
std::string m_devTrainId;
std::string m_devIp;//sockaddr_in
...The compiler's message:
Linking...
CDeviceFileParser.obj : error LNK2005: "int a" (?a@@3HA) already defined in CCommsManager.obj
CSnapLogger.obj : error LNK2005: "int a" (?a@@3HA) already defined in CCommsManager.obj
snaplogger_main.obj : error LNK2005: "int a" (?a@@3HA) already defined in CCommsManager.objsnaplogger_main.h includes CSnapLogger.h CSnapLogger.h includes CCommsManager.h CCommsManager.h includes CDeviceFileParser.h
-
If you want to use this variable only in this source module then add the
static
keyword thus:static int a;
Just say 'NO' to evaluated arguments for diadic functions! Ash
-
This problem probably sounds silly but I can't get my head round it; there it goes:
#include <DelimFileParser.h>
int a; //I can't define any type of variable in here
typedef struct
{
std::string m_devTrainId;
std::string m_devIp;//sockaddr_in
...The compiler's message:
Linking...
CDeviceFileParser.obj : error LNK2005: "int a" (?a@@3HA) already defined in CCommsManager.obj
CSnapLogger.obj : error LNK2005: "int a" (?a@@3HA) already defined in CCommsManager.obj
snaplogger_main.obj : error LNK2005: "int a" (?a@@3HA) already defined in CCommsManager.objsnaplogger_main.h includes CSnapLogger.h CSnapLogger.h includes CCommsManager.h CCommsManager.h includes CDeviceFileParser.h
If you want a global variable to be accessed from all files, make the definition in the source file (.c, .cpp etc.) instead of a header file. In the header do a declaration like this -
extern int a;
«_Superman_»
I love work. It gives me something to do between weekends.