Solved

multiple defines....option to override?

Posted on 2006-10-26
6
190 Views
Last Modified: 2010-04-21
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!
0
Comment
Question by:PhilC
  • 2
  • 2
6 Comments
 
LVL 43

Expert Comment

by:ravenpl
ID: 17817882
make both int iSomeVar; static
static int iSomeVar;
static means visible only to this .cpp file, and not exported(not visible during linking)
0
 

Author Comment

by:PhilC
ID: 17818180
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
 
LVL 43

Expert Comment

by:ravenpl
ID: 17818203
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
 

Author Comment

by:PhilC
ID: 17818283
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
 
LVL 34

Accepted Solution

by:
Duncan Roe earned 500 total points
ID: 17827909
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now