what's the expected behavior for duplicate symbol

S

SamL

I have two compilation units which define two variables of the same
name but of different types. The program compiled by xlc and gcc shows
different behaviors. I am wondering what the C standard says about
duplicate symbols. Thanks.

$ cat a.c
#include <stdio.h>
double a=0;
void b() {
a=1.444;
printf ("a=%lg &a=%p\n", a, &a);
}

$ cat b.c
#include <stdio.h>

int a=2;
void b();

int main() {
printf ("a=%d &a=%p\n", a, &a);
b();
return 0;
}

$ xlc a.c b.c
a.c:
b.c:
ld: 0711-224 WARNING: Duplicate symbol: a
ld: 0711-345 Use the -bloadmap or -bnoquiet option to obtain more
information.

$ a.out
a=0 &a=200005c8
a=1.444 &a=200005c8

$ gcc a.c b.c
ld: 0711-224 WARNING: Duplicate symbol: a
ld: 0711-345 Use the -bloadmap or -bnoquiet option to obtain more
information.

$ a.out
a=2 &a=20000d18
a=1.444 &a=20000d24
 
H

Harald van Dijk

I have two compilation units which define two variables of the same name
but of different types. The program compiled by xlc and gcc shows
different behaviors. I am wondering what the C standard says about
duplicate symbols. Thanks.

It says:

6.2.2p2:
"In the set of translation units and libraries that constitutes an entire
program, each declaration of a particular identifier with external
linkage denotes the same object or function."

6.2.7p2:
"All declarations that refer to the same object or function shall have
compatible type; otherwise, the behavior is undefined."

In other words, the standard does not specify the behaviour, and
compilers are allowed to differ in how they treat duplicate definitions,
and in whether they allow it at all.
 
E

Eric Sosman

SamL said:
I have two compilation units which define two variables of the same
name but of different types. The program compiled by xlc and gcc shows
different behaviors. I am wondering what the C standard says about
duplicate symbols. Thanks.

The behavior is undefined, so the compilers' differing
behavior does not mean that either of them is wrong. In
principle, anything at all could happen, with or without
any kind of error or warning message to alert you.
 
S

SamL

It says:

6.2.2p2:
"In the set of translation units and libraries that constitutes an entire
program, each declaration of a particular identifier with external
linkage denotes the same object or function."

6.2.7p2:
"All declarations that refer to the same object or function shall have
compatible type; otherwise, the behavior is undefined."

In other words, the standard does not specify the behaviour, and
compilers are allowed to differ in how they treat duplicate definitions,
and in whether they allow it at all.

Thanks for your reply. This is the answer I am looking for.
 
C

CBFalconer

SamL said:
I have two compilation units which define two variables of the
same name but of different types. The program compiled by xlc
and gcc shows different behaviors. I am wondering what the C
standard says about duplicate symbols. Thanks.

$ cat a.c
#include <stdio.h>
double a=0;
void b() {
a=1.444;
printf ("a=%lg &a=%p\n", a, &a);
}

$ cat b.c
#include <stdio.h>

int a=2;
void b();

int main() {
printf ("a=%d &a=%p\n", a, &a);
b();
return 0;
}

If you want to use those 'global' variables locally in the
individual files, simply mark them as static. This says that the
names are not exported from the compilation unit involved, and now
there is no conflict. (Something should show up at the linking
stage as it is.)
 

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,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top