Link to home
Start Free TrialLog in
Avatar of PhilC
PhilC

asked on

multiple defines....option to override?

we are converting our code to compile under g++ instead of gcc
under gcc we have module first.c that includes int iSomeVar;
then in second.c we may have static int iSomeVar;
this compiles and runs fine with the local iSomVar being used throughout second.c
with g++ we get a link error saying multiple definition of iSomeVar
is there some way to make g++ work like gcc in this respect?
Thank You!
Avatar of ravenpl
ravenpl
Flag of Poland image

make both int iSomeVar; static
static int iSomeVar;
static means visible only to this .cpp file, and not exported(not visible during linking)
Avatar of PhilC
PhilC

ASKER

I was hoping for a compiler option since this situation arises in much of our code, and the global variable is used throughout the rest of the library, and included in headers as extern
thank you!
In linking phase there may be only one instance of some global symbol (in this case its 'iSomeVar') So only one of them may be not static.
Or puth them into different namespaces. But anyway - it requires You to change the sources rather than using linker flag.
Avatar of PhilC

ASKER

There is only one non-static, in first.c.  It is then used in other modules through first.h which has extern in iSomeVar
thank you!
ASKER CERTIFIED SOLUTION
Avatar of Duncan Roe
Duncan Roe
Flag of Australia 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