Solved

multiple defines....option to override?

Posted on 2006-10-26
6
185 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
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…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

771 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

12 Experts available now in Live!

Get 1:1 Help Now