lwinkenb
asked on
curious about multiple defined symbols.
Let's say I have a header file, that is included by two different .cpp files.
//test.h
#ifndef TEST_H
#define TEST_H
int g_int;
#endif
//test1.cpp
#include "test.h"
extern int g_int;
...
//test2.cpp
#include "test.h"
extern int g_int;
I am getting the link error LNK2005: "int g_int" (?g_int@@3HA) already defined in test1.obj
Now I thought that declaring them as extern in the .cpp files would tell the compiler that the g_int variable was already declared externally. I know that I can move the g_int declaration to the .cpp file and it will work fine. I was just curious if there was any way at all to have a global variable in a header file.
//test.h
#ifndef TEST_H
#define TEST_H
int g_int;
#endif
//test1.cpp
#include "test.h"
extern int g_int;
...
//test2.cpp
#include "test.h"
extern int g_int;
I am getting the link error LNK2005: "int g_int" (?g_int@@3HA) already defined in test1.obj
Now I thought that declaring them as extern in the .cpp files would tell the compiler that the g_int variable was already declared externally. I know that I can move the g_int declaration to the .cpp file and it will work fine. I was just curious if there was any way at all to have a global variable in a header file.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER