?
Solved

multiple defines....option to override?

Posted on 2006-10-26
6
Medium Priority
?
214 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 35

Accepted Solution

by:
Duncan Roe earned 2000 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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

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…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month9 days, 17 hours left to enroll

762 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