Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > how to use the keyword extern in c?

Reply
Thread Tools

how to use the keyword extern in c?

 
 
ooze
Guest
Posts: n/a
 
      08-25-2004
hi all ,
anyone could tell me the useage of the keyworkd "extern" in c? and
the difference between c & cplusplus?

in the C99Rationalv5.10.pdf it lists 4 cases. while the first
common cases will incur the vc compiler to complain.
 
Reply With Quote
 
 
 
 
Jack Klein
Guest
Posts: n/a
 
      08-25-2004
On 24 Aug 2004 19:15:30 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) (ooze) wrote in
comp.lang.c:

> hi all ,
> anyone could tell me the useage of the keyworkd "extern" in c?


The extern keyword specifies that the identifier being declared has
external linkage. This allows an object or function to be defined in
one translation unit (basically, source file and everything it
includes) and referred to by name from code in other translation
units.

> and
> the difference between c & cplusplus?


C++ is off-topic here, news:comp.lang.c++ is down the hall to the
right.

> in the C99Rationalv5.10.pdf it lists 4 cases. while the first
> common cases will incur the vc compiler to complain.


As to what Visual C++ might complain about, it might make a difference
whether you are using it as a C or C++ compiler, as it handles both
languages.

The document you reference is not necessarily one that many people
have.

Even for those who do, like I do, your reference is far too vague.
The document is well over 200 pages, and the word 'extern' appears
many times. I spent a brief time looking in both the PDF and printed
versions I have, and did not find the section you are referring to.

Post again and include the specific section number from the document.
That is, if you are indeed compiling C code and not C++. And for
readers here who do not have a copy of the rationale, copy and paste
the code sample you are referring to, and copy and paste the compiler
or linker error messages as well.

--
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.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
 
Reply With Quote
 
 
 
 
ooze
Guest
Posts: n/a
 
      08-25-2004
Jack Klein <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>. ..
> On 24 Aug 2004 19:15:30 -0700, (E-Mail Removed) (ooze) wrote in
> comp.lang.c:
>
> > hi all ,
> > anyone could tell me the useage of the keyworkd "extern" in c?

>
> The extern keyword specifies that the identifier being declared has
> external linkage. This allows an object or function to be defined in
> one translation unit (basically, source file and everything it
> includes) and referred to by name from code in other translation
> units.
>
> > and
> > the difference between c & cplusplus?

>
> C++ is off-topic here, news:comp.lang.c++ is down the hall to the
> right.
>
> > in the C99Rationalv5.10.pdf it lists 4 cases. while the first
> > common cases will incur the vc compiler to complain.

>
> As to what Visual C++ might complain about, it might make a difference
> whether you are using it as a C or C++ compiler, as it handles both
> languages.
>
> The document you reference is not necessarily one that many people
> have.
>
> Even for those who do, like I do, your reference is far too vague.
> The document is well over 200 pages, and the word 'extern' appears
> many times. I spent a brief time looking in both the PDF and printed
> versions I have, and did not find the section you are referring to.
>
> Post again and include the specific section number from the document.
> That is, if you are indeed compiling C code and not C++. And for
> readers here who do not have a copy of the rationale, copy and paste
> the code sample you are referring to, and copy and paste the compiler
> or linker error messages as well.



hallo,

in section 6.2.2 Linkages of identifiers
there is a table listed the 4 cases also includes detail info.

what's the difference when the keyword "extern" appears and when it doest appear?
for instance,
+++++++++++++++++++++++++++++++
extern int var1; | int var1;
_______________________________
extern void func(); | void func();

in a c file.
 
Reply With Quote
 
ooze
Guest
Posts: n/a
 
      08-25-2004
Jack Klein <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>. ..
> On 24 Aug 2004 19:15:30 -0700, (E-Mail Removed) (ooze) wrote in
> comp.lang.c:
>
> > hi all ,
> > anyone could tell me the useage of the keyworkd "extern" in c?

>
> The extern keyword specifies that the identifier being declared has
> external linkage. This allows an object or function to be defined in
> one translation unit (basically, source file and everything it
> includes) and referred to by name from code in other translation
> units.
>
> > and
> > the difference between c & cplusplus?

>
> C++ is off-topic here, news:comp.lang.c++ is down the hall to the
> right.
>
> > in the C99Rationalv5.10.pdf it lists 4 cases. while the first
> > common cases will incur the vc compiler to complain.

>
> As to what Visual C++ might complain about, it might make a difference
> whether you are using it as a C or C++ compiler, as it handles both
> languages.
>
> The document you reference is not necessarily one that many people
> have.
>
> Even for those who do, like I do, your reference is far too vague.
> The document is well over 200 pages, and the word 'extern' appears
> many times. I spent a brief time looking in both the PDF and printed
> versions I have, and did not find the section you are referring to.
>
> Post again and include the specific section number from the document.
> That is, if you are indeed compiling C code and not C++. And for
> readers here who do not have a copy of the rationale, copy and paste
> the code sample you are referring to, and copy and paste the compiler
> or linker error messages as well.




hallo,

in section 6.2.2 Linkages of identifiers
there is a table listed the 4 cases also includes detail info.

what's the difference when the keyword "extern" appears and when it doest appear?
for instance,
++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++
extern int var1; | int var1;
__________________________________________________ ___________
extern int var1 = 0x0bad | int var1 = 0x0bad
__________________________________________________ ___________
extern void func(); | void func();
++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++

in a c file.
 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      08-25-2004
In <(E-Mail Removed) > (E-Mail Removed) (ooze) writes:

>what's the difference when the keyword "extern" appears and when it doest appear?
>for instance,


Assuming all declarations have file scope:

>+++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++
> extern int var1; | int var1;


declaration, external linkage tentative external definition

>_________________________________________________ ____________
>extern int var1 = 0x0bad; | int var1 = 0x0bad;


external definition external definition

>_________________________________________________ ____________
>extern void func(); | void func();


declaration, external linkage declaration, external linkage

As you can see, the only context where extern makes any difference is
in object declarations that are not intended to be definitions. Otherwise
it can be omitted in file scope declarations, because it is the default.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: (E-Mail Removed)
 
Reply With Quote
 
ooze
Guest
Posts: n/a
 
      08-26-2004
(E-Mail Removed) (Dan Pop) wrote in message news:<cgiamm$q3q$(E-Mail Removed)>...
> In <(E-Mail Removed) > (E-Mail Removed) (ooze) writes:
>
> >what's the difference when the keyword "extern" appears and when it doest appear?
> >for instance,

>
> Assuming all declarations have file scope:
>
> >+++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++
> > extern int var1; | int var1;

>
> declaration, external linkage tentative external definition
>
> >_________________________________________________ ____________
> >extern int var1 = 0x0bad; | int var1 = 0x0bad;

>
> external definition external definition
>
> >_________________________________________________ ____________
> >extern void func(); | void func();

>
> declaration, external linkage declaration, external linkage
>
> As you can see, the only context where extern makes any difference is
> in object declarations that are not intended to be definitions. Otherwise
> it can be omitted in file scope declarations, because it is the default.
>
> Dan


thanks, as you stated,
I remember in C the defintion of an object can ONLY occur ONLY ONCE.

but if I write extern int var1 = 0x0bad ; in one.c,
and write extern int var1 = 0xdead; in another.c
the vc c compiler will happy accept it.

since you think

> >_________________________________________________ ____________
> >extern int var1 = 0x0bad; | int var1 = 0x0bad;

>
> external definition external definition


how to explain it?

thanks
 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      08-26-2004
On 25 Aug 2004 18:37:26 -0700, (E-Mail Removed) (ooze) wrote in
comp.lang.c:

> (E-Mail Removed) (Dan Pop) wrote in message news:<cgiamm$q3q$(E-Mail Removed)>...
> > In <(E-Mail Removed) > (E-Mail Removed) (ooze) writes:
> >
> > >what's the difference when the keyword "extern" appears and when it doest appear?
> > >for instance,

> >
> > Assuming all declarations have file scope:
> >
> > >+++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++
> > > extern int var1; | int var1;

> >
> > declaration, external linkage tentative external definition
> >
> > >_________________________________________________ ____________
> > >extern int var1 = 0x0bad; | int var1 = 0x0bad;

> >
> > external definition external definition
> >
> > >_________________________________________________ ____________
> > >extern void func(); | void func();

> >
> > declaration, external linkage declaration, external linkage
> >
> > As you can see, the only context where extern makes any difference is
> > in object declarations that are not intended to be definitions. Otherwise
> > it can be omitted in file scope declarations, because it is the default.
> >
> > Dan

>
> thanks, as you stated,
> I remember in C the defintion of an object can ONLY occur ONLY ONCE.
>
> but if I write extern int var1 = 0x0bad ; in one.c,
> and write extern int var1 = 0xdead; in another.c
> the vc c compiler will happy accept it.
>
> since you think
>
> > >_________________________________________________ ____________
> > >extern int var1 = 0x0bad; | int var1 = 0x0bad;

> >
> > external definition external definition

>
> how to explain it?
>
> thanks


There are a lot of technical details about different models of linking
that is used by the linkers of different implementations on different
platforms. Some of them will allow multiple definitions of an
external symbol, as long as no more than one of them has an
initializer.

The C standard requires that any object or function with external
linkage that is not actually referenced in the program must have
exactly 0 or 1 definitions somewhere in the program. Any object
or function with external linkage that is actually referenced in a
program must have exactly 1 definition, no more and no less.

A violation of this requirement causes undefined behavior, and
compilers are never required to diagnose undefined behavior.

Any compiler should be able to compile the two source files with two
external definitions of 'var1', both with initializers. The error
should come when the two object files are linked together to make the
program.

I don't know of any version of Microsoft's Visual C++, or any other C
compiler for that matter, that will not generate an error if those two
source files are put into a single program.

Here is what I got from Visual C++ 6.0:

--------------------Configuration: multi - Win32
Debug--------------------
Linking...
multi2.obj : error LNK2005: _var1 already defined in multi1.obj
multi___Win32_Debug/multi.exe : fatal error LNK1169: one or more
multiply defined symbols found
Error executing link.exe.

multi.exe - 2 error(s), 0 warning(s)

And here is what I got from the Visual C++ 2005 Express Edition Beta:

------ Build started: Project: multi, Configuration: Debug Win32
------
Compiling...
multi2.c
Linking...
multi1.obj : error LNK2005: _var1 already defined in multi2.obj
Debug/multi.exe : fatal error LNK1169: one or more multiply defined
symbols found
Build log was saved at "file://c:\Program Files\Microsoft Visual
Studio 8\projects\multi\multi\Debug\BuildLog.htm"
multi - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped
==========

--
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.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
 
Reply With Quote
 
ooze
Guest
Posts: n/a
 
      08-26-2004
Jack Klein <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>. ..
> On 25 Aug 2004 18:37:26 -0700, (E-Mail Removed) (ooze) wrote in
> comp.lang.c:
>
> > (E-Mail Removed) (Dan Pop) wrote in message news:<cgiamm$q3q$(E-Mail Removed)>...
> > > In <(E-Mail Removed) > (E-Mail Removed) (ooze) writes:
> > >
> > > >what's the difference when the keyword "extern" appears and when it doest appear?
> > > >for instance,
> > >
> > > Assuming all declarations have file scope:
> > >
> > > >+++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++
> > > > extern int var1; | int var1;
> > >
> > > declaration, external linkage tentative external definition
> > >
> > > >_________________________________________________ ____________
> > > >extern int var1 = 0x0bad; | int var1 = 0x0bad;
> > >
> > > external definition external definition
> > >
> > > >_________________________________________________ ____________
> > > >extern void func(); | void func();
> > >
> > > declaration, external linkage declaration, external linkage
> > >
> > > As you can see, the only context where extern makes any difference is
> > > in object declarations that are not intended to be definitions. Otherwise
> > > it can be omitted in file scope declarations, because it is the default.
> > >
> > > Dan

> >
> > thanks, as you stated,
> > I remember in C the defintion of an object can ONLY occur ONLY ONCE.
> >
> > but if I write extern int var1 = 0x0bad ; in one.c,
> > and write extern int var1 = 0xdead; in another.c
> > the vc c compiler will happy accept it.
> >
> > since you think
> >
> > > >_________________________________________________ ____________
> > > >extern int var1 = 0x0bad; | int var1 = 0x0bad;
> > >
> > > external definition external definition

> >
> > how to explain it?
> >
> > thanks

>
> There are a lot of technical details about different models of linking
> that is used by the linkers of different implementations on different
> platforms. Some of them will allow multiple definitions of an
> external symbol, as long as no more than one of them has an
> initializer.
>
> The C standard requires that any object or function with external
> linkage that is not actually referenced in the program must have
> exactly 0 or 1 definitions somewhere in the program. Any object
> or function with external linkage that is actually referenced in a
> program must have exactly 1 definition, no more and no less.
>
> A violation of this requirement causes undefined behavior, and
> compilers are never required to diagnose undefined behavior.
>
> Any compiler should be able to compile the two source files with two
> external definitions of 'var1', both with initializers. The error
> should come when the two object files are linked together to make the
> program.
>
> I don't know of any version of Microsoft's Visual C++, or any other C
> compiler for that matter, that will not generate an error if those two
> source files are put into a single program.
>
> Here is what I got from Visual C++ 6.0:
>
> --------------------Configuration: multi - Win32
> Debug--------------------
> Linking...
> multi2.obj : error LNK2005: _var1 already defined in multi1.obj
> multi___Win32_Debug/multi.exe : fatal error LNK1169: one or more
> multiply defined symbols found
> Error executing link.exe.
>
> multi.exe - 2 error(s), 0 warning(s)
>
> And here is what I got from the Visual C++ 2005 Express Edition Beta:
>
> ------ Build started: Project: multi, Configuration: Debug Win32
> ------
> Compiling...
> multi2.c
> Linking...
> multi1.obj : error LNK2005: _var1 already defined in multi2.obj
> Debug/multi.exe : fatal error LNK1169: one or more multiply defined
> symbols found
> Build log was saved at "file://c:\Program Files\Microsoft Visual
> Studio 8\projects\multi\multi\Debug\BuildLog.htm"
> multi - 2 error(s), 0 warning(s)
> ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped
> ==========



what's the difference between
"tentative external definition" &

"external definition"?

vc will accept if one object defined twice with the above respectivly.
 
Reply With Quote
 
ooze
Guest
Posts: n/a
 
      08-26-2004
Jack Klein <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>. ..
> On 25 Aug 2004 18:37:26 -0700, (E-Mail Removed) (ooze) wrote in
> comp.lang.c:
>
> > (E-Mail Removed) (Dan Pop) wrote in message news:<cgiamm$q3q$(E-Mail Removed)>...
> > > In <(E-Mail Removed) > (E-Mail Removed) (ooze) writes:
> > >
> > > >what's the difference when the keyword "extern" appears and when it doest appear?
> > > >for instance,
> > >
> > > Assuming all declarations have file scope:
> > >
> > > >+++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++
> > > > extern int var1; | int var1;
> > >
> > > declaration, external linkage tentative external definition
> > >
> > > >_________________________________________________ ____________
> > > >extern int var1 = 0x0bad; | int var1 = 0x0bad;
> > >
> > > external definition external definition
> > >
> > > >_________________________________________________ ____________
> > > >extern void func(); | void func();
> > >
> > > declaration, external linkage declaration, external linkage
> > >
> > > As you can see, the only context where extern makes any difference is
> > > in object declarations that are not intended to be definitions. Otherwise
> > > it can be omitted in file scope declarations, because it is the default.
> > >
> > > Dan

> >
> > thanks, as you stated,
> > I remember in C the defintion of an object can ONLY occur ONLY ONCE.
> >
> > but if I write extern int var1 = 0x0bad ; in one.c,
> > and write extern int var1 = 0xdead; in another.c
> > the vc c compiler will happy accept it.
> >
> > since you think
> >
> > > >_________________________________________________ ____________
> > > >extern int var1 = 0x0bad; | int var1 = 0x0bad;
> > >
> > > external definition external definition

> >
> > how to explain it?
> >
> > thanks

>
> There are a lot of technical details about different models of linking
> that is used by the linkers of different implementations on different
> platforms. Some of them will allow multiple definitions of an
> external symbol, as long as no more than one of them has an
> initializer.
>
> The C standard requires that any object or function with external
> linkage that is not actually referenced in the program must have
> exactly 0 or 1 definitions somewhere in the program. Any object
> or function with external linkage that is actually referenced in a
> program must have exactly 1 definition, no more and no less.
>
> A violation of this requirement causes undefined behavior, and
> compilers are never required to diagnose undefined behavior.
>
> Any compiler should be able to compile the two source files with two
> external definitions of 'var1', both with initializers. The error
> should come when the two object files are linked together to make the
> program.
>
> I don't know of any version of Microsoft's Visual C++, or any other C
> compiler for that matter, that will not generate an error if those two
> source files are put into a single program.
>
> Here is what I got from Visual C++ 6.0:
>
> --------------------Configuration: multi - Win32
> Debug--------------------
> Linking...
> multi2.obj : error LNK2005: _var1 already defined in multi1.obj
> multi___Win32_Debug/multi.exe : fatal error LNK1169: one or more
> multiply defined symbols found
> Error executing link.exe.
>
> multi.exe - 2 error(s), 0 warning(s)
>
> And here is what I got from the Visual C++ 2005 Express Edition Beta:
>
> ------ Build started: Project: multi, Configuration: Debug Win32
> ------
> Compiling...
> multi2.c
> Linking...
> multi1.obj : error LNK2005: _var1 already defined in multi2.obj
> Debug/multi.exe : fatal error LNK1169: one or more multiply defined
> symbols found
> Build log was saved at "file://c:\Program Files\Microsoft Visual
> Studio 8\projects\multi\multi\Debug\BuildLog.htm"
> multi - 2 error(s), 0 warning(s)
> ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped
> ==========



if int var1; defined in one.c, and extern int var1=23; defined in
another.c.
vc will accept it.
vc will also accept
1) int var1 in one.c and int var1 in another.c
2) int var1 in one.c and int var1 = 23 in another.c

it seems to me that in a program an object can have mutiple tentative
definitions, but only one external definition.
 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      08-26-2004
In <(E-Mail Removed) > (E-Mail Removed) (ooze) writes:

>if int var1; defined in one.c, and extern int var1=23; defined in
>another.c.
>vc will accept it.
>vc will also accept
>1) int var1 in one.c and int var1 in another.c
>2) int var1 in one.c and int var1 = 23 in another.c


Assuming all declarations have file scope, there are two external
definitions for var1 in all your examples.

>it seems to me that in a program an object can have mutiple tentative
>definitions, but only one external definition.


Only fools try to figure out how the language works from the behaviour of
their compilers. If multiple definitions of an object cause undefined
behaviour, any implementation is free to silently accept them. And,
according to Annex J.2 (I'm too lazy to search the chapter and verse):

- An identifier with external linkage is used, but in the program
there does not exist exactly one external definition for the
identifier, or the identifier is not used and there exist
multiple external definitions for the identifier (6.9).

So, VC is free to silently accept as many external definitions for var1
as you provide. But the behaviour of your program is undefined, if it
provides more than one.

A tentative definition becomes a genuine definition at the end of the
translation unit, if not superseded by another definition for the same
object. This is why it is called "tentative" definition. So, your
conclusion is valid only for one translation unit (C source file plus
whatever stuff is included via #include directives), not for a complete
program containing multiple definitions for the same object in multiple
translation units.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: (E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Difference of extern short *x and extern short x[]? Andre C Programming 5 07-17-2012 07:38 PM
Use of 'extern' keyword siliconwafer C Programming 5 07-31-2005 03:39 AM
extern const char * vs. extern const char []http://tinyurl.com/47e3k Thomas Matthews C++ 5 08-02-2004 10:36 AM
keyword extern tweak C Programming 18 06-29-2004 11:20 AM



Advertisments