473,385 Members | 1,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

external variables

Hi all. I am porting a C program I wrote to C++. I am having some issues
with external variables. In C, I can have global variables declared in
common header files, but I get "multiple definition" errors when compiling as
c++. Here's an example:
/* BEGIN myhdr.h */
int GLOBAL_INT;
void func();
/* END myhdr.h */

/* BEGIN main.cpp */
#include"myhdr.h"
int main()
{
GLOBAL_INT = 1;
func();
return (0);
}
/* END main.cpp */

/* BEGIN func.cpp */
#include"myhdr.h"
void func()
{
GLOBAL_INT = 2;
}
/* END func.cpp */

g++ -c main.cpp
g++ -c func.cpp
g++ main.o func.o

func.o(.bss+0x0): multiple definition of `GLOBAL_INT'
main.o(.bss+0x0): first defined here
gmake: *** [test] Error 1
How can I get around this issue?

Thanks,
-Adam

Jul 22 '05 #1
6 2143
Adam Bozanich wrote:
Hi all. I am porting a C program I wrote to C++. I am having some issues
with external variables. In C, I can have global variables declared in
common header files, but I get "multiple definition" errors when compiling as
c++. Here's an example: [snip] How can I get around this issue?


Use "extern int GLOBAL_INT;" to declare the variable in the header file.
Provide a definition, "int GLOBAL_INT;", in exactly one source file.

--
Regards,
Buster.
Jul 22 '05 #2

"Adam Bozanich" <ab******@ccsf.edu> wrote in message
news:Pi**************************************@hill s.ccsf.cc.ca.us...
Hi all. I am porting a C program I wrote to C++. I am having some issues
with external variables. In C, I can have global variables declared in
common header files, but I get "multiple definition" errors when compiling as c++. Here's an example:


You don't understand the difference between a declaration and a definition,
even your question mixes up these two different things.

There is little difference here between C and C++ so I don't know what
compiler you are using that lets you have multiple definitions in C, either
you are mistaken or its a seriously bad C compiler. The rules in C and C++
are essentially the same, you can only have one definition but as many
declarations as you like.

int GLOBAL_INT;

The above is a definition, don't put it in a header file, put it in one
source file.

extern int GLOBAL_INT;

The above is a declaration, put it in a common header file.

You'll find that give or take a wrinkle or two, the same applies to C and
C++.

john
Jul 22 '05 #3
John Harrison wrote:
The rules in C and C++
are essentially the same, you can only have one definition but as many
declarations as you like.


No. In C there are tentative definitions.

http://h30097.www3.hp.com/docs/base_...E/DOCU_022.HTM

--
Regards,
Buster.
Jul 22 '05 #4

"Buster" <no***@nowhere.com> wrote in message
news:c6**********@news8.svr.pol.co.uk...
John Harrison wrote:
The rules in C and C++
are essentially the same, you can only have one definition but as many
declarations as you like.
No. In C there are tentative definitions.

http://h30097.www3.hp.com/docs/base_...E/DOCU_022.HTM
--
Regards,
Buster.


Would two identical tentative definitions in different translations units
cause a multiple definition error? I can see from the link that they would
be OK in the same translation unit.

The OP was claiming two tentative definitions in different translation units
is OK in C, which seemed unlikely to me.

john
Jul 22 '05 #5
On Tue, 20 Apr 2004 20:11:56 +0100, Buster <no***@nowhere.com> wrote
in comp.lang.c++:
John Harrison wrote:
The rules in C and C++
are essentially the same, you can only have one definition but as many
declarations as you like.


No. In C there are tentative definitions.

http://h30097.www3.hp.com/docs/base_...E/DOCU_022.HTM


You misunderstand tentative definitions in C. Tentative definitions
are a concept that exists within a single translation unit only, they
do not extend to linkage. At the end of a translation unit, if no
non-tentative definition has appeared for a tentative one, the
tentative one is turned into a specific definition.

The OP's sample creates multiple definitions for the int variable
object, one for each translation unit that includes the header. This
happens to link without error on some implementations due to the model
used by the linker.

Nevertheless, providing more than one definition for an object with
external linkage in a C program specifically invokes undefined
behavior, and this is what the OP's sample does.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 22 '05 #6
Jack Klein wrote:
On Tue, 20 Apr 2004 20:11:56 +0100, Buster <no***@nowhere.com> wrote
in comp.lang.c++:
John Harrison wrote:
The rules in C and C++
are essentially the same, you can only have one definition but as many
declarations as you like.


No. In C there are tentative definitions.

http://h30097.www3.hp.com/docs/base_...E/DOCU_022.HTM


You misunderstand tentative definitions in C. Tentative definitions
are a concept that exists within a single translation unit only, they
do not extend to linkage. At the end of a translation unit, if no
non-tentative definition has appeared for a tentative one, the
tentative one is turned into a specific definition.

The OP's sample creates multiple definitions for the int variable
object, one for each translation unit that includes the header. This
happens to link without error on some implementations due to the model
used by the linker.

Nevertheless, providing more than one definition for an object with
external linkage in a C program specifically invokes undefined
behavior, and this is what the OP's sample does.


I'll take your word for it. Thanks! And sorry, everyone, for confusing
matters.

--
Regards,
Buster.
Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Newsnet Customer | last post by:
Hi, Is an external variable a variable that is defined outside of a method? or a variable with the extern modifier prefixed to it? and what's the difference with external and global variables? ...
0
by: Ida | last post by:
Hi, I am trying to build an dll with Microsoft Visual C++ but during the linking phase I get linking errors. Script.obj : error LNK2019: unresolved external symbol __imp__PyString_AsString...
47
by: Richard Hayden | last post by:
Hi, I have the following code: /******************************** file1.c #include <iostream> extern void dummy(); inline int testfunc() {
2
by: Lu | last post by:
Hello, I am wondering how to protect a global variable in a header file from external access. So I googled and found: "The keyword 'static' has two different uses, depending on whether it is...
19
by: J. J. Farrell | last post by:
After many years of dealing with definition and linkage issues in ways that I know to be safe, I've decided it's time to try to understand this area properly. Consider a header file with the file...
0
by: DDE | last post by:
Hi all, I have an application (number of web Services) in which I define different application variables, like application etc. Besides that, I have a couple of assemblies containing classes and...
2
by: Deb M. | last post by:
I am trying to find out how I can use the ASP.net framework yet still post the variables to an external script (not ASPX) after the form has been submitted. So, the ASPX page will still post to...
2
by: ras26 | last post by:
I have a WebSite "App1" which has an external assembly "Lib1". Inside this external assembly we have developed some classes that derive from "System.Web.UI.Page". One of these pages is called...
11
by: Huayang Xia | last post by:
What will the following piece of code print? (10 or 15) def testClosure(maxIndex) : def closureTest(): return maxIndex maxIndex += 5 return closureTest()
2
by: =?Utf-8?B?YmFzaA==?= | last post by:
Hello, I am compiling a CPP code using Visual studion .net 2003. I get the following error, despite having windldap.h and wldap32.dll in my include and lib paths. Here is the error. uuid.lib...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.