external variables

A

Adam Bozanich

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
 
B

Buster

Adam said:
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.
 
J

John Harrison

Adam Bozanich said:
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
 
J

John Harrison

J

Jack Klein


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.
 
B

Buster

Jack said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top