473,799 Members | 3,382 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

External object definitions

Hi to all,

To explain my question I'll help myself
with the following two code snippets:

/*** file1.c ***/

#include <stdio.h>

void print_it(void);
int i;

int main(void)
{
print_it();
printf("main i = %d\n", i);

return 0;
}

/*** file2.c ***/

#include <stdio.h>

int i;

void print_it(void)
{
i = 2;
printf("print_i t i = %d\n", i);
}
As you can see I have a
tentative definition for object `i',
in both TUs.
According to C99 6.9.2 p2, one can deduce
that both of these tentative definitions, will
turn into real definitions at the end of the
TUs with initializer zero.

Then I think that in this case I will be having,
*two definitions* for the same identifier with
external linkage!
Then I would be violating a semantics constraint.
Specifically the one pointed out in C99 6.9 p5.

To my surprise, I didn't receive any diagnostics
from the compiler. I compiled this program using
this command line:

$ gcc -g -Wall -ansi -pedantic -o filex file1.c file2.c

I even ran the executable generated, and it ran OK:
$ filex
print_it i = 2
main i = 2

What am I getting wrong?
What is my error?
What am I misunderstandin g here?

Thank you very much in advance,

Max
Nov 13 '05 #1
9 4200
maxw_cc wrote:
Then I think that in this case I will be having,
*two definitions* for the same identifier with
external linkage!


No, the two identifiers designate the same object.
Default initialization is for the object, not for
each designation.

Nov 13 '05 #2
On 12 Aug 2003 23:50:28 -0700, ma*****@yahoo.c om (maxw_cc) wrote in
comp.lang.c:
Hi to all,

To explain my question I'll help myself
with the following two code snippets:

/*** file1.c ***/

#include <stdio.h>

void print_it(void);
int i;

int main(void)
{
print_it();
printf("main i = %d\n", i);

return 0;
}

/*** file2.c ***/

#include <stdio.h>

int i;

void print_it(void)
{
i = 2;
printf("print_i t i = %d\n", i);
}
As you can see I have a
tentative definition for object `i',
in both TUs.
According to C99 6.9.2 p2, one can deduce
that both of these tentative definitions, will
turn into real definitions at the end of the
TUs with initializer zero.

Then I think that in this case I will be having,
*two definitions* for the same identifier with
external linkage!
Then I would be violating a semantics constraint.
Specifically the one pointed out in C99 6.9 p5.
There is no such thing as a "semantics constraint". The constraints
in 6.9 are paragraphs 2 and 3. Paragraphs 4 and 5 are in the
semantics section, and are not constraints.
To my surprise, I didn't receive any diagnostics
from the compiler. I compiled this program using
this command line:
The compiler by definition deals with one translation unit at a time.
$ gcc -g -Wall -ansi -pedantic -o filex file1.c file2.c

I even ran the executable generated, and it ran OK:
$ filex
print_it i = 2
main i = 2

What am I getting wrong?
What is my error?
What am I misunderstandin g here?

Thank you very much in advance,

Max


When your separately compiled translation units are linked together by
your linker, you are indeed violating the terms of 6.9 p5.

Specifically the wording "If an identifier declared with external
linkage is used in an expression (other than as part of the operand of
a sizeof operator whose result is an integer constant), somewhere in
the entire program there shall be exactly one external definition for
the identifier; otherwise, there shall be no more than one."

But this is not a constraint violation, because it is not in a
constraints section, but a semantics one.

So what applies here is p2 of section 4:

"If a ‘‘shall’’ or ‘‘shall not’’ requirement that appears outside of a
constraint is violated, the behavior is undefined."

Your program invokes undefined behavior, which requires neither a
diagnostic nor any particular results, as far as the standard is
concerned.

Different languages have different conceptual models of what C calls
external linkage. Some of them require the behavior that you see,
where there can be multiple definitions of an external symbol so long
as no more than one of them contain an initializer. FORTRAN named
common blocks need this, for example. So the linkers of some tools
work this way. Essentially they pass the concept of tentative
definition on to the linker.

C allows but does not require this feature by making the results of
such a program undefined.

--
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++ ftp://snurse-l.org/pub/acllc-c++/faq
Nov 13 '05 #3
ma*****@yahoo.c om (maxw_cc) wrote:

As you can see I have a
tentative definition for object `i',
in both TUs.
According to C99 6.9.2 p2, one can deduce
that both of these tentative definitions, will
turn into real definitions at the end of the
TUs with initializer zero.

Then I think that in this case I will be having,
*two definitions* for the same identifier with
external linkage!
Then I would be violating a semantics constraint.
Specifically the one pointed out in C99 6.9 p5.
As explained in another followup, this results in undefined behaviour.
To my surprise, I didn't receive any diagnostics
from the compiler.


This is one of the "common implementation extensions", see J.5.11
"multiple external definitions":

[#1] There may be more than one external definition for the
identifier of an object, with or without the explicit use of
the keyword extern; if the definitions disagree, or more
than one is initialized, the behavior is undefined (6.9.2).

Tony.
--
f.a.n.finch <do*@dotat.at > http://dotat.at/
FAIR ISLE: VARIABLE BECOMING NORTHWESTERLY 3 OR 4 INCREASING 5 TO 7. RAIN THEN
SQUALLY SHOWERS. MODERATE BECOMING GOOD.
Nov 13 '05 #4
In message <Q9************ ********@comcas t.com>, Douglas A. Gwyn
<DA****@null.ne t> writes
maxw_cc wrote:
Then I think that in this case I will be having,
*two definitions* for the same identifier with
external linkage!


No, the two identifiers designate the same object.
Default initialization is for the object, not for
each designation.


This is one of the areas of C that has always left me deeply
uncomfortable. As I understand it, the programmer who decides to provide
explicit initialisation transforms the two declarations into two
distinct objects. I have always thought that the idea is deeply flawed.
It breaks the general description that variables are, by default,
defined and pure declarations need to be made using extern.
--
ACCU Spring Conference 2003 April 2-5
The Conference you should not have missed
ACCU Spring Conference 2004 Late April
Francis Glassborow ACCU

Nov 13 '05 #5
In <bh**********@n ews-reader5.wanadoo .fr> "jacob navia" <ja*********@ja cob.remcomp.fr> writes:
When I wrote the linker of lcc-win32 I was confronted to this problem.
All linkers have this quite uncredible behavior:

File1.c
int table[2];

File2.c
int table[834];
int main(void { }

This will link without any warnings, even with

gcc -pedantic f1.c 2.c
It's undefined behaviour, so why do you expect a diagnostic? Besides,
-pedantic has nothing to do with the linking stage.
In my linker, I added a warning, but still left this as is. I am still wandering
why this behavior is continued.

Should I discontinue this? Should I give an error rather than just
a warning???
The C standard is giving you free hand. A diagnostic is certainly
helpful, it doesn't really matter whether it's an error or a warning
(any user ignoring such a warning gets exactly what he deserves).
P.S. All this happens only with bss variables of course. If you mix
bss with initialized data variables the behavior is even stranger!


Undefined behaviour can never be too strange ;-)

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #6
jacob navia a écrit :
When I wrote the linker of lcc-win32 I was confronted to this problem.
All linkers have this quite uncredible behavior:

File1.c
int table[2];

File2.c
int table[834];
int main(void { }

This will link without any warnings, even with

gcc -pedantic f1.c 2.c
-Wl,--warn-common is the option that gives a warning with gcc, for this kind
of code.
In my linker, I added a warning, but still left this as is. I am still
wandering why this behavior is continued.


It seems to be a common practice in the unix world. According to ld manual :
--warn-common
Warn when a common symbol is combined with another common symbol
or with a symbol definition. Unix linkers allow this somewhat
sloppy practice, but linkers on some other operating systems do
not. This option allows you to find potential problems from com-
bining global symbols. Unfortunately, some C libraries use this
practice, so you may get some warnings about symbols in the
libraries as well as in your programs.

--
Richard
Nov 13 '05 #7
Richard Delorme <ab****@nospam. fr> writes:

|> jacob navia a écrit :
|>
|> > When I wrote the linker of lcc-win32 I was confronted to this problem.
|> > All linkers have this quite uncredible behavior:
|> >
|> > File1.c
|> > int table[2];
|> >
|> > File2.c
|> > int table[834];
|> > int main(void { }
|> >
|> > This will link without any warnings, even with
|> >
|> > gcc -pedantic f1.c 2.c
|>
|> -Wl,--warn-common is the option that gives a warning with gcc, for this kind
|> of code.

Another option is to compile with -fno-common, which will cause the
linker to error out on the multiple definitions of table.

Andreas.

--
Andreas Schwab, SuSE Labs, sc****@suse.de
SuSE Linux AG, Deutschherrnstr . 15-19, D-90429 Nürnberg
Key fingerprint = 58CA 54C7 6D53 942B 1756 01D3 44D5 214B 8276 4ED5
"And now for something completely different."
Nov 13 '05 #8
Tony Finch <do*@dotat.at > wrote in message news:<Vo******* @news.chiark.gr eenend.org.uk>. ..
This is one of the "common implementation extensions", see J.5.11
"multiple external definitions":

[#1] There may be more than one external definition for the
identifier of an object, with or without the explicit use of
the keyword extern; if the definitions disagree, or more
than one is initialized, the behavior is undefined (6.9.2).

Tony.

First of all thanks to all of you for your good inputs.
Especially thank you very much to Jack Klein and Tony
Finch, you've been of great help.

However, I still wonder why they put 6.9.2 in the last
sentence of J.5.11 p1. I see this as if they were telling me
to look at 6.9.2 to find justifications on why having discrepant
definitions or having multiple external definitions (not tentative)
invoke UB. It would have made more sense to me if they
had put 6.9 and/or probably 6.2.2.

Thanks again for all your help,

Max
Nov 13 '05 #9
In article <bh**********@n ews-reader5.wanadoo .fr>, jacob navia
<ja*********@ja cob.remcomp.fr> writes
When I wrote the linker of lcc-win32 I was confronted to this problem.
All linkers have this quite uncredible behavior:

File1.c
int table[2];

File2.c
int table[834];
int main(void { }

This will link without any warnings, even with

gcc -pedantic f1.c 2.c


This dates back to early versions of Fortran (my X3.9-1966 isn't to
hand, so I can't say if it was standardised or not), where you could
write:

COMMON /TABLE/ T(2)

in one subroutine and:

COMMON /TABLE/ T(834)

in another. The linker would assign enough memory for the largest
version of each common block.

--
Clive D.W. Feather, writing for himself | Home: <cl***@davros.o rg>
Tel: +44 20 8371 1138 (work) | Web: <http://www.davros.org>
Fax: +44 870 051 9937 | Work: <cl***@demon.ne t>
Written on my laptop; please observe the Reply-To address
Nov 13 '05 #10

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

Similar topics

0
2843
by: Putz Ronald | last post by:
Hy! I am trying to convert a win Oracle application to solaris! We have use external c functions which I have ibncluded in a shared object. See the makefile below: OBJECTS = definitions.o eventlog.o arc_xpi.o OracleFuncs.o DEFS = -DOS_SOLARIS -fPIC -G
6
3424
by: Christopher Benson-Manica | last post by:
Just FMI, if an external stylesheet is linked to a page (with <link>), will subsequent <style> tags override the values in the external stylesheet? -- Christopher Benson-Manica | I *should* know what I'm talking about - if I ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
6
2165
by: Adam Bozanich | last post by:
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 */
47
3886
by: Richard Hayden | last post by:
Hi, I have the following code: /******************************** file1.c #include <iostream> extern void dummy(); inline int testfunc() {
19
2920
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.
4
2741
by: Peter Ammon | last post by:
I would like to share a variable between two functions defined in two separate source files; no other functions will need the global variable so I'd prefer to not give it file scope. Thus, I want a variable with local scope, external linkage, and static storage. I believe I can do this for the file that does not define the variable by declaring it inside a function. Can I also avoid giving it file scope in the file that does define the...
0
9688
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...
1
10238
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
9077
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
7570
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
6809
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
5467
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...
0
5589
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3761
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2941
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.