473,403 Members | 2,359 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,403 software developers and data experts.

when should multiple definitions of global variables trigger link time errors?

Hi!

When are multiple definitions of global variables with the same name
considered legal in C, and how is it different from C++? It appears
that in terms of assembly language, some C definitions are translated
to "weak" symbols, in which case multiple definitions are merged by the
linker, while some become "normal" symbols, triggering linker errors in
case they are defined more then once. Is it true that in C++, multiple
definitions are always disallowed?

This code (2 translation units) compiles with gcc -ansi, but triggers
linker errors with g++:

//weak1.c (or weak1.cpp):
#include <stdio.h>
int a[5];
void f();
int main()
{
printf("&a=%p\n",a);
f();
}

//weak2.c (or weak2.cpp):
#include <stdio.h>
int a[10];
void f()
{
printf("f: &a=%p\n",a);
}

Compiled with gcc, it prints:

&a=0x80495e0
f: &a=0x80495e0

- showing that the definitions `int a[5]' and `int a[10]' were merged.
A symbol table listing shows that the dimension of a[] in the linked
binary is 10. Adding initializers to the definitions, for example,
triggers linker errors in C.

Thanks in advance!
Yossi

Mar 4 '06 #1
8 3272
yo***********@gmail.com wrote:
Hi!

When are multiple definitions of global variables with the same name
considered legal in C,
Never.
and how is it different from C++?
Not topical on comp.lang.c, but I would expect it to be the same.
It appears
that in terms of assembly language, some C definitions are translated
to "weak" symbols, in which case multiple definitions are merged by the
linker, while some become "normal" symbols, triggering linker errors in
case they are defined more then once.
That may be the case on some implementations, but it is not true on all.
On one of the C implementations I use regularly it will produce a link
time error on multiple definitions of a symbol, on another it won't.
That's the wonder of Undefined Behaviour, *anything* can happen.
Is it true that in C++, multiple
definitions are always disallowed?

This code (2 translation units) compiles with gcc -ansi, but triggers
linker errors with g++:
Use different options with gcc and you may get different results.
//weak1.c (or weak1.cpp):
#include <stdio.h>
int a[5];
<snip>
//weak2.c (or weak2.cpp):
#include <stdio.h>
int a[10];
<snip>

Undefined behaviour in C. Anything can happen.

<snip>
binary is 10. Adding initializers to the definitions, for example,
triggers linker errors in C.


So will selecting the correct option in some versions of gcc.
Specifically the -fno-common option. You should also use -pedantic if
you want gcc to be a fully compliant C compiler, and looking at and
enabling the other warnings would also be a good thing.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
Mar 4 '06 #2
On 4 Mar 2006 04:34:34 -0800, in comp.lang.c , yo***********@gmail.com
wrote:
Hi!

When are multiple definitions of global variables with the same name
considered legal in C,
Never, there can be only one definition of any object.

Multiple global declarations are fine though, so long as they're the
same. Your example with two different declarations of 'a' should have
triggered a warning from your compiler, and is I believe undefined
behaviour.
and how is it different from C++?


No idea.

Mark McIntyre
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Mar 4 '06 #3
On Sat, 04 Mar 2006 14:44:54 +0000, in comp.lang.c , Flash Gordon
<sp**@flash-gordon.me.uk> wrote:
yo***********@gmail.com wrote:
Hi!

When are multiple definitions of global variables with the same name
considered legal in C,

Use different options with gcc and you may get different results.
//weak1.c (or weak1.cpp):
#include <stdio.h>
int a[5];


<snip>
//weak2.c (or weak2.cpp):
#include <stdio.h>
int a[10];


<snip>

Undefined behaviour in C. Anything can happen.


IIRC these are declarations and tentative definitions. The fact that
they're different is indeed UB but its not illegal to have several
tentative definitions.
Mark McIntyre
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Mar 4 '06 #4
> IIRC these are declarations and tentative definitions. The fact that
they're different is indeed UB but its not illegal to have several
tentative definitions.


Could you please explain that? If I understand correctly, you say that
`int a[5]' in both translation units are "declarations and tentative
definitions", but I don't understand exactly what that means. Does it
mean that multiple definitions are going to be allowed by the compiler,
but the behaviour is only defined when they are identical (if so,
identical on what level)? Which definitions are tentative?

TIA,
Yossi

Mar 4 '06 #5
Thanks!

I use -Wall -ansi -pedantic. I assumed it does the trick. Forgive me if
it's the wrong place to ask, but while we're at it - what are the
correct options to ask gcc for it's fullest standard compliance? It
looks like -ansi -pedantic are not enough.

Or is it standard compliant to ignore multiple definitions of a[],
especially if I fixed them to define the same array size? Do any
multiple definitions trigger undefined behaviour, or only different
ones?

Mar 4 '06 #6
Mark McIntyre <ma**********@spamcop.net> writes:
On Sat, 04 Mar 2006 14:44:54 +0000, in comp.lang.c , Flash Gordon
<sp**@flash-gordon.me.uk> wrote:
yo***********@gmail.com wrote:
Hi!

When are multiple definitions of global variables with the same name
considered legal in C,

Use different options with gcc and you may get different results.
//weak1.c (or weak1.cpp):
#include <stdio.h>
int a[5];


<snip>
//weak2.c (or weak2.cpp):
#include <stdio.h>
int a[10];


<snip>

Undefined behaviour in C. Anything can happen.


IIRC these are declarations and tentative definitions. The fact that
they're different is indeed UB but its not illegal to have several
tentative definitions.


This is only true within the same translation unit. A tentative
definition still causes the object to be defined at the end of the
unit. Since both units define it, this causes UB regardless of
disagreement on type.

-Micah
Mar 4 '06 #7
yo***********@gmail.com wrote:

Could you please explain that? If I understand correctly, you say that
`int a[5]' in both translation units are "declarations and tentative
definitions", but I don't understand exactly what that means.


A tentative definition is a declaration that will be interpreted as a
definition only if no other (non-tentative) definition appears in the
translation unit. In your case, there are no other definitions in the
translation units, so both of the tentative definitions are, in fact,
interpreted as definitions, resulting in multiple definitions for the
same object, which results in undefined behavior. The code is
incorrect, but there's no obligation for the compiler/linker to diagnose
it.

-Larry Jones

I've got an idea for a sit-com called "Father Knows Zilch." -- Calvin
Mar 4 '06 #8
On Sat, 04 Mar 2006 21:13:54 GMT, in comp.lang.c ,
la************@ugs.com wrote:
yo***********@gmail.com wrote:

Could you please explain that? If I understand correctly, you say that
`int a[5]' in both translation units are "declarations and tentative
definitions", but I don't understand exactly what that means.


A tentative definition is a declaration that will be interpreted as a
definition only if no other (non-tentative) definition appears in the
translation unit.


Agh, for some reason i had it in my head that the rule was across all
TUs.
Mark McIntyre
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Mar 4 '06 #9

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

Similar topics

4
by: SilverShadow | last post by:
Hello, I'm having trouble with something that may be easily remedied. I use Cantera running on Python. I need to make multiple "Reactor()" objects and have them assigned a different (user...
20
by: weston | last post by:
I've got a piece of code where, for all the world, it looks like this fails in IE 6: hometab = document.getElementById('hometab'); but this succeeds: hometabemt =...
9
by: yossi.kreinin | last post by:
Hi! When are multiple definitions of global variables with the same name considered legal in C, and how is it different from C++? It appears that in terms of assembly language, some C...
0
by: capes | last post by:
have functions in my cell model calling CVODE for each time step. When I build the application I get a number of link errors: \cardiacsimulator.o Release\ccardiaccell.o Release\ccelldata.o...
10
by: subramanian100in | last post by:
Suppose I declare a global variable int g; in two different files say a.c which has main() and b.c When I compile them to build an executable under gcc in Redhat Linux with the command ...
6
by: Gaijinco | last post by:
I'm having a weird error compiling a multiple file project: I have three files: tortuga.h where I have declared 5 global variables and prototypes for some functions. tortuga.cpp where I...
6
by: stenasc | last post by:
Hi, In a file test1.c, in have the two functions f1 and f2. These are declared in the header file t1.h. I want to call these functions in another file subprog.c, which contains the function...
20
by: teddysnips | last post by:
Weird. I have taken over responsibility for a legacy application, Access 2k3, split FE/BE. The client has reported a problem and I'm investigating. I didn't write the application. The...
4
by: icarus | last post by:
global_vars.py has the global variables set_var.py changes one of the values on the global variables (don't close it or terminate) get_var.py retrieves the recently value changed (triggered right...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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...

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.