Link to home
Start Free TrialLog in
Avatar of lwinkenb
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.
ASKER CERTIFIED SOLUTION
Avatar of brettmjohnson
brettmjohnson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lwinkenb
lwinkenb

ASKER

Thanks a lot.