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!
PhilCAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Duncan RoeConnect With a Mentor Software DeveloperCommented:
In the file with static int iSomeVar, change all instances of iSomeVar, to some other name, such as static_iSomeVar. It seems C let you shadow the extern declaration with a static, but C++ doesn't.
Renaming will fix the g++ build without breaking the gcc build. And tou will improve maintainability - when you grep for iSomeVar, you'll now see the difference between the global and local variants.
If there are a lot of vars to do, so much the better when you've done them.
0
 
ravenplCommented:
make both int iSomeVar; static
static int iSomeVar;
static means visible only to this .cpp file, and not exported(not visible during linking)
0
 
PhilCAuthor Commented:
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!
0
 
ravenplCommented:
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.
0
 
PhilCAuthor Commented:
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!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.