473,811 Members | 4,047 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2166
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******** *************** *************** @hills.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.po l.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.l earn.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
declaratio ns 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
17823
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? cheers -
0
2814
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 referenced in function "public: static struct _object * __cdecl SomeFunctionName Script.obj : error LNK2019: unresolved external symbol __imp__PyObject_Repr referenced in function "public: static struct _object * __cdecl
47
3888
by: Richard Hayden | last post by:
Hi, I have the following code: /******************************** file1.c #include <iostream> extern void dummy(); inline int testfunc() {
2
9088
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 applied to an external variable or function or to an automatic variable. When applied to an external variable (global) variable the scope of that variable to the file in which it is declared. This is useful to hide buffers and variables that are used...
19
2924
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 scope declaration int i; This header is included in two files that refer to i but do not declare it. The two files build together into a single program.
0
1235
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 STATIC methods used by those web services. I can't find any way to access my application variables from those methods definde in the external assemblies. ex: in my external assembly, i have the following:
2
4708
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 itself, but then I need to transfer the variables to another page. I need to do this transfer using the POST method and not the GET method. I tried user Server.Transfer("http://localhost/test.asp") but it retuns an error of...
2
2087
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 "WebNotification". In the WebSite "App1" we create an instance of "WebNotification", but when this class tries to access some Session variables it generates the following error: Session state can only be used when enableSessionState is set to...
11
3030
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
2721
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 rpcrt4.lib ole32.lib oleaut32.lib uuid.lib Creating library libsq00.lib and object libsq00.exp libq00.lib(ootb.obj) : error LNK2019: unresolved external symbol _ldap_unbind@4
0
9724
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9604
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10394
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9201
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7665
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6882
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5552
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3863
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.