473,704 Members | 2,626 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"extern" meaning

Hi all,
I am reading "C: A Reference Manual" 4th ed and I get lost for the
"extern". It says that global object without specifying the
storage-class specifier will have "extern" as the default storage-class
specifier. My (little) C experience tells me that an object with
"extern" is to let the linker knows that the object is referencing the
object defined in somewhere, and this "somewhere" object does not have
the storage-class specifier "extern". That is:
<------------ file A ------------>
int A;
...
A=10;
<------------ file B ------------>
extern int A;
if (A)
...

But if the default storage-class specifier is "extern", then
variable "int A" is, indeed, "extern int A". Then who is the definiting
occurence?
Please correct me if there is a mis-concept.

Nov 14 '05 #1
19 3854
ccwork wrote:
Hi all,
I am reading "C: A Reference Manual" 4th ed and I get lost for the
"extern". It says that global object without specifying the
storage-class specifier will have "extern" as the default storage-class specifier. My (little) C experience tells me that an object with
"extern" is to let the linker knows that the object is referencing the object defined in somewhere, and this "somewhere" object does not have the storage-class specifier "extern". That is:
<------------ file A ------------>
int A;
...
A=10;
<------------ file B ------------>
extern int A;
if (A)
...

But if the default storage-class specifier is "extern", then
variable "int A" is, indeed, "extern int A". Then who is the definiting occurence?
Please correct me if there is a mis-concept.


'extern' specifies external linkage for an identifier, nothing more.
It makes the identifier known to the linker so it could be referenced.

The definition of variable A occurs in file "A".

P.Krumins

Nov 14 '05 #2
ccwork wrote:
I am reading "C: A Reference Manual" 4th ed and I get lost for the
"extern". It says that global object without specifying the
storage-class specifier will have "extern" as the default storage-class
specifier. My (little) C experience tells me that an object with
"extern" is to let the linker knows that the object is referencing the
object defined in somewhere, and this "somewhere" object does not have
the storage-class specifier "extern". That is:
<------------ file A ------------>
int A;
...
A=10;
<------------ file B ------------>
extern int A;
if (A)
...

But if the default storage-class specifier is "extern", then
variable "int A" is, indeed, "extern int A". Then who is the definiting
occurence?
Please correct me if there is a mis-concept.


C language doesn't not rely on the concept of "linker". There's no
"linker" in C. At language level, external linkage of an identifier
means just one thing - that this identifier refers to the same object
(or function) in all translation units. How this requirement is
implemented in practice (using that "linker" thingy or something else)
is an implementation detail, a completely different story, irrelevant to
the language itself.

If you really want to think about this in terms of "exporting/importing"
access to the object, think of it this way: when 'extern' is included in
a _definition_ of some object, it "exports" the corresponding identifier
to the outside world (i.e. other translation units), but when 'extern'
is included in a mere _declaration_ of an object, it "imports" the
identifier from the outside world.

In your case 'int A' in file 'A' is a definition of an identifier 'A'
attached to an object of type 'int'. This identifier has external
linkage by default, meaning that identifier 'A' is "exported", made
accessible from other translation units. In order to access this object
other translation units have to "import" this identifier by providing an
'extern' _declaration_ for it. That's exactly what you have in file 'B'.

--
Best regards,
Andrey Tarasevich
Nov 14 '05 #3
Andrey Tarasevich wrote:

C language doesn't not rely on the concept of "linker".
[I presume you didn't mean to add the extra 'not'.]
There's no "linker" in C.
So what's translation phase 8 all about then?
At language level, external linkage of an identifier means just
one thing - that this identifier refers to the same object (or
function) in all translation units. How this requirement is
implemented in practice (using that "linker" thingy or something
else) is an implementation detail, a completely different story,
irrelevant to the language itself.


"Library components are linked..."

How does an implementation link _without_ a linker?

Even C interpreters must have a linker, just as they must have
a function call stack.

--
Peter

Nov 14 '05 #4

Andrey Tarasevich wrote:
ccwork wrote:
I am reading "C: A Reference Manual" 4th ed and I get lost for the "extern". It says that global object without specifying the
storage-class specifier will have "extern" as the default storage-class specifier. My (little) C experience tells me that an object with
"extern" is to let the linker knows that the object is referencing the object defined in somewhere, and this "somewhere" object does not have the storage-class specifier "extern". That is:
<------------ file A ------------>
int A;
...
A=10;
<------------ file B ------------>
extern int A;
if (A)
...

But if the default storage-class specifier is "extern", then
variable "int A" is, indeed, "extern int A". Then who is the definiting occurence?
Please correct me if there is a mis-concept.
C language doesn't not rely on the concept of "linker". There's no
"linker" in C. At language level, external linkage of an identifier
means just one thing - that this identifier refers to the same object
(or function) in all translation units. How this requirement is
implemented in practice (using that "linker" thingy or something

else) is an implementation detail, a completely different story, irrelevant to the language itself.

If you really want to think about this in terms of "exporting/importing" access to the object, think of it this way: when 'extern' is included in a _definition_ of some object, it "exports" the corresponding identifier to the outside world (i.e. other translation units), but when 'extern' is included in a mere _declaration_ of an object, it "imports" the
identifier from the outside world.

In your case 'int A' in file 'A' is a definition of an identifier 'A'
attached to an object of type 'int'. This identifier has external
linkage by default, meaning that identifier 'A' is "exported", made
accessible from other translation units. In order to access this object other translation units have to "import" this identifier by providing an 'extern' _declaration_ for it. That's exactly what you have in file

'B'.

That's the problem. Since global variable is regraded as "extern"
(is this right?), in other words, file A means:
<------------ file A ------------>
extern int A;
...
A=10;
<------------ file B ------------>
extern int A;
if (A)
...

Then how do I know which one is the defining occurrence of A? Does
the assignment on A=10 specify the declaraction "extern int A" in file
A is the definition? Or else?

Nov 14 '05 #5


if both files have variabled defined as "extern" , there is no harm if
one is of the form:

extern int a =1;

i will try on rh9 to test it.

Nov 14 '05 #6
ccwork wrote:
...
In your case 'int A' in file 'A' is a definition of an identifier 'A'
attached to an object of type 'int'. This identifier has external
linkage by default, meaning that identifier 'A' is "exported", made
accessible from other translation units. In order to access this object
other translation units have to "import" this identifier by providing

an
'extern' _declaration_ for it. That's exactly what you have in file

'B'.

That's the problem. Since global variable is regraded as "extern"
(is this right?), in other words, file A means:
<------------ file A ------------>
extern int A;
...
A=10;
<------------ file B ------------>
extern int A;
if (A)
...


No. The original 'int A' was a _definition_ of an object with external
linkage. 'extern int A' is a non-defining _declaration_ of an identifier
with external linkage. These are not the same.

'int A' defines an object with external linkage, but that doesn't mean
that you can just add an explicit 'extern' keyword here and get the same
thing. Adding 'extern' to 'int A' will have an unwanted side effect of
turning this definition into a non-defining declaration.
Then how do I know which one is the defining occurrence of A?
Neither. 'A' is not defined in the above files.
Does
the assignment on A=10 specify the declaraction "extern int A" in file
A is the definition?


Assignment? How do intend to use assignment here?

If you include an initializer into the declaration, this declaration
will become a definition, even if you specify an explicit 'extern'

extern int A; // non-defining declaration
extern int A = 0; // definition

--
Best regards,
Andrey Tarasevich

Nov 14 '05 #7
ccwork wrote:
Andrey Tarasevich wrote:
ccwork wrote:
I am reading "C: A Reference Manual" 4th ed and I get lost for
the
"extern". It says that global object without specifying the
storage-class specifier will have "extern" as the default
storage-class
specifier. My (little) C experience tells me that an object with
"extern" is to let the linker knows that the object is referencing
the
object defined in somewhere, and this "somewhere" object does not
have
the storage-class specifier "extern". That is:
<------------ file A ------------>
int A;
...
A=10;
<------------ file B ------------>
extern int A;
if (A)
...

But if the default storage-class specifier is "extern", then
variable "int A" is, indeed, "extern int A". Then who is the
definiting
occurence?
Please correct me if there is a mis-concept.


C language doesn't not rely on the concept of "linker". There's no
"linker" in C. At language level, external linkage of an identifier
means just one thing - that this identifier refers to the same object
(or function) in all translation units. How this requirement is
implemented in practice (using that "linker" thingy or something


else)
is an implementation detail, a completely different story, irrelevant


to
the language itself.

If you really want to think about this in terms of


"exporting/importing"
access to the object, think of it this way: when 'extern' is included


in
a _definition_ of some object, it "exports" the corresponding


identifier
to the outside world (i.e. other translation units), but when


'extern'
is included in a mere _declaration_ of an object, it "imports" the
identifier from the outside world.

In your case 'int A' in file 'A' is a definition of an identifier 'A'
attached to an object of type 'int'. This identifier has external
linkage by default, meaning that identifier 'A' is "exported", made
accessible from other translation units. In order to access this


object
other translation units have to "import" this identifier by providing


an
'extern' _declaration_ for it. That's exactly what you have in file


'B'.

That's the problem. Since global variable is regraded as "extern"
(is this right?), in other words, file A means:
<------------ file A ------------>
extern int A;
...
A=10;
<------------ file B ------------>
extern int A;
if (A)
...

Then how do I know which one is the defining occurrence of A? Does
the assignment on A=10 specify the declaraction "extern int A" in file
A is the definition? Or else?


'extern' indicates to the compiler the actual storage space is allocated
elsewhere. Because now your example (is slightly different to above) has
no 'int A', only 'extern int A', no storage space is actually created for A.

Hence at link time there will be a failure as A is not defined anywhere.

As for A=10, that doesn't actually allocate space, this is just an
assignment, it has nothing to do with the definition of A.
Nov 14 '05 #8
On Tue, 05 Apr 2005 18:06:28 -0700, ccwork wrote:
Hi all,
I am reading "C: A Reference Manual" 4th ed and I get lost for the
"extern". It says that global object without specifying the
storage-class specifier will have "extern" as the default storage-class
specifier.
C doesn't define the term "global". It is commonly used and misused in
different and contradictory ways. I assume the text above is referring to
declarations at file scope i.e. outside of any function.
My (little) C experience tells me that an object with
"extern" is to let the linker knows that the object is referencing the
object defined in somewhere, and this "somewhere" object does not have
the storage-class specifier "extern". That is:
<------------ file A ------------>
int A;
...
A=10;
<------------ file B ------------>
extern int A;
if (A)
...

But if the default storage-class specifier is "extern", then
variable "int A" is, indeed, "extern int A". Then who is the definiting
occurence?


It is incorrect to say that the default storage-class specifier is extern,
however it would be correct to say that the default linkage for file scope
declarations is external. The presence of extern in file B above makes a
difference, extern int A is just a declaration, it doesn't act as a
definition of A. int A; does (technically it is called a tentative
definition but the end resukt is that it is a definition here). Note that
providing an initialiser forces it to be a full definition, e.g.

extern int A = 1; /* Is a full definition */

Lawrence
Nov 14 '05 #9
On 5 Apr 2005 21:18:19 -0700, in comp.lang.c , "Peter Nilsson"
<ai***@acay.com .au> wrote:
How does an implementation link _without_ a linker?
who knows - maybe it does runtime binding, like, er most OSen do...
Even C interpreters must have a linker, just as they must have
a function call stack


Must they? Does it say that in the standard? .

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt >
Nov 14 '05 #10

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

Similar topics

4
4255
by: Sergei | last post by:
I ran into this problem. I needed to create an entry for access to a library of functions that are "extern C", and I just can't find the right syntax, if it exists at all ( I am using MSVC 6.0-7.1).. Note, that basically I don't need an "extern C" linkage, I just need to define a type to cast a resolved function address.. Any ideas? Basically , I need something like this..
1
2648
by: terrencel | last post by:
I was told to look at some old C code that was ported to C++. One of the file is like: ========================================= CPPClass* someCPPVar = NULL; extern "C" {
111
6423
by: JKop | last post by:
Okay here we go, I feel it's about time people conversed about the bullshit aspects of C++ (including the bullshit stuff brought forward from C). I'll begin with a few of my own grievances: 1) The whole "function declaration Vs object definition" fiasco, which results in the following (temporary creating) syntax: Blah poo = Blah();
8
2180
by: Generic Usenet Account | last post by:
Our C++ program was linked with some legacy C functions. We had used the extern "C" declaration, and everything was working fine. Then someone thought that it would be better to have a consistent file naming convention and renamed the files with the legacy C functions from .c to .cxx. Lo and behold we started getting linker errors left and right. When we reverted back to the old naming convention, the problem went away. Also, the...
10
6187
by: Mark A. Gibbs | last post by:
I have a question about mixing C and C++. In a C++ translation unit, I want to define a function with internal linkage and C calling convention. Here's a sample of what I want to do: // main.cpp // This is defined in a C module extern "C" void fake_qsort(void*, std::size_t, std::size_t, int (*compare)(const void*, const void*));
12
2725
by: G Patel | last post by:
I've seen some code with extern modifiers in front of variables declared inside blocks. Are these purely definitions (no definition) or are they definitions with static duration but external linkage? Not much on this in the FAQ or tutorials.
10
3512
by: Rick Anderson | last post by:
All, I am receiving the following compilation error on LINUX (but not Solaris, HPUX, WIN32, etc): compiling osr.c LBFO.h(369): warning #64: declaration does not declare anything extern struct foobar; ^
4
4754
by: kk_oop | last post by:
Hi. I need to write a C++ callback function and register it with a C program. I've read that this can be done if the callback function is a static method. I've also read that I should use a global function with the extern "C" prefix. I was leaning toward using the static method approach until I saw a posting that said compatibility between static C++ functions and C is not guaranteed in the respective language standards. This made me...
4
6255
by: mimi | last post by:
The programmer indicates to the compiler that a function is written in a different programming language using a linkage directives.It is intuitive that extern "SomeLanguage" is used to declare functions written in the "SomeLanguage". But I am quite confused what information does the linkage directive tells the compiler.The "generated" function name? The way the arguments are ordered?Or something else? And I am still wondering why extern...
0
8687
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9272
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9135
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9026
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
8977
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7891
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
6605
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
5931
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();...
2
2483
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.