Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #103611 > unrolled thread

Is 'extern' necessary?

Started byBartC <bc@freeuk.com>
First post2017-02-10 17:16 +0000
Last post2017-02-11 03:17 -0800
Articles 20 on this page of 105 — 19 participants

Back to article view | Back to comp.lang.c


Contents

  Is 'extern' necessary? BartC <bc@freeuk.com> - 2017-02-10 17:16 +0000
    Re: Is 'extern' necessary? Keith Thompson <kst-u@mib.org> - 2017-02-10 09:33 -0800
      Re: Is 'extern' necessary? BartC <bc@freeuk.com> - 2017-02-10 18:41 +0000
        Re: Is 'extern' necessary? Keith Thompson <kst-u@mib.org> - 2017-02-10 12:09 -0800
          Re: Is 'extern' necessary? BartC <bc@freeuk.com> - 2017-02-10 20:29 +0000
            Re: Is 'extern' necessary? Keith Thompson <kst-u@mib.org> - 2017-02-10 13:26 -0800
              Re: Is 'extern' necessary? BartC <bc@freeuk.com> - 2017-02-10 22:56 +0000
                Re: Is 'extern' necessary? jameskuyper@verizon.net - 2017-02-10 20:19 -0800
                  Re: Is 'extern' necessary? "James R. Kuyper" <jameskuyper@verizon.net> - 2017-02-11 09:53 -0500
                Re: Is 'extern' necessary? Tim Rentsch <txr@alumni.caltech.edu> - 2017-02-13 05:19 -0800
        Re: Is 'extern' necessary? jameskuyper@verizon.net - 2017-02-10 12:09 -0800
          Re: Is 'extern' necessary? BartC <bc@freeuk.com> - 2017-02-10 20:50 +0000
            Re: Is 'extern' necessary? supercat@casperkitty.com - 2017-02-10 13:09 -0800
            Re: Is 'extern' necessary? Keith Thompson <kst-u@mib.org> - 2017-02-10 13:33 -0800
            Re: Is 'extern' necessary? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-10 23:23 +0000
              Re: Is 'extern' necessary? BartC <bc@freeuk.com> - 2017-02-10 23:35 +0000
                Re: Is 'extern' necessary? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-11 00:34 +0000
              Re: Is 'extern' necessary? David Brown <david.brown@hesbynett.no> - 2017-02-13 12:41 +0100
                Re: Is 'extern' necessary? BartC <bc@freeuk.com> - 2017-02-13 13:14 +0000
                  Re: Is 'extern' necessary? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-13 14:05 +0000
                  Re: Is 'extern' necessary? jameskuyper@verizon.net - 2017-02-13 07:49 -0800
            Re: Is 'extern' necessary? jameskuyper@verizon.net - 2017-02-10 19:33 -0800
              Re: Is 'extern' necessary? supercat@casperkitty.com - 2017-02-11 09:41 -0800
                Re: Is 'extern' necessary? Keith Thompson <kst-u@mib.org> - 2017-02-11 10:19 -0800
                  Re: Is 'extern' necessary? "James R. Kuyper" <jameskuyper@verizon.net> - 2017-02-11 13:39 -0500
                  Re: Is 'extern' necessary? supercat@casperkitty.com - 2017-02-12 10:10 -0800
        Re: Is 'extern' necessary? Andrey Tarasevich <andreytarasevich@hotmail.com> - 2017-02-10 14:59 -0800
        Re: Is 'extern' necessary? David Brown <david.brown@hesbynett.no> - 2017-02-13 12:33 +0100
    Re: Is 'extern' necessary? Siri Cruise <chine.bleu@yahoo.com> - 2017-02-10 09:35 -0800
      Re: Is 'extern' necessary? Keith Thompson <kst-u@mib.org> - 2017-02-10 12:12 -0800
    Re: Is 'extern' necessary? jameskuyper@verizon.net - 2017-02-10 10:38 -0800
    Re: Is 'extern' necessary? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-10 21:10 +0000
      Re: Is 'extern' necessary? supercat@casperkitty.com - 2017-02-10 13:25 -0800
    Re: Is 'extern' necessary? Andrey Tarasevich <andreytarasevich@hotmail.com> - 2017-02-10 14:53 -0800
      Re: Is 'extern' necessary? Andrey Tarasevich <andreytarasevich@hotmail.com> - 2017-02-10 15:06 -0800
      Re: Is 'extern' necessary? BartC <bc@freeuk.com> - 2017-02-10 23:24 +0000
        Re: Is 'extern' necessary? Ian Collins <ian-news@hotmail.com> - 2017-02-11 12:34 +1300
          Re: Is 'extern' necessary? David Brown <david.brown@hesbynett.no> - 2017-02-13 13:14 +0100
            Re: Is 'extern' necessary? GOTHIER Nathan <nathan.gothier@gmail.com> - 2017-02-13 13:44 +0100
              Re: Is 'extern' necessary? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-13 14:13 +0000
              Re: Is 'extern' necessary? David Brown <david.brown@hesbynett.no> - 2017-02-13 15:16 +0100
                Re: Is 'extern' necessary? GOTHIER Nathan <nathan.gothier@gmail.com> - 2017-02-13 17:24 +0100
                  Re: Is 'extern' necessary? "James R. Kuyper" <jameskuyper@verizon.net> - 2017-02-13 14:24 -0500
                  Re: Is 'extern' necessary? David Brown <david.brown@hesbynett.no> - 2017-02-13 23:29 +0100
              Re: Is 'extern' necessary? Keith Thompson <kst-u@mib.org> - 2017-02-13 11:06 -0800
              Re: Is 'extern' necessary? Andrey Tarasevich <andreytarasevich@hotmail.com> - 2017-02-13 11:15 -0800
            Re: Is 'extern' necessary? "James R. Kuyper" <jameskuyper@verizon.net> - 2017-02-13 11:48 -0500
              Re: Is 'extern' necessary? supercat@casperkitty.com - 2017-02-13 09:03 -0800
              Re: Is 'extern' necessary? David Brown <david.brown@hesbynett.no> - 2017-02-13 23:37 +0100
        Re: Is 'extern' necessary? Andrey Tarasevich <andreytarasevich@hotmail.com> - 2017-02-10 15:40 -0800
        Re: Is 'extern' necessary? Tim Rentsch <txr@alumni.caltech.edu> - 2017-02-13 05:09 -0800
          Re: Is 'extern' necessary? BartC <bc@freeuk.com> - 2017-02-13 13:46 +0000
            Re: Is 'extern' necessary? Keith Thompson <kst-u@mib.org> - 2017-02-13 09:52 -0800
              Re: Is 'extern' necessary? BartC <bc@freeuk.com> - 2017-02-13 19:19 +0000
                Re: Is 'extern' necessary? Keith Thompson <kst-u@mib.org> - 2017-02-13 11:34 -0800
                  Re: Is 'extern' necessary? BartC <bc@freeuk.com> - 2017-02-13 20:13 +0000
                Re: Is 'extern' necessary? Andrey Tarasevich <andreytarasevich@hotmail.com> - 2017-02-13 13:11 -0800
                Re: Is 'extern' necessary? David Brown <david.brown@hesbynett.no> - 2017-02-14 00:33 +0100
                  Re: Is 'extern' necessary? BartC <bc@freeuk.com> - 2017-02-14 01:02 +0000
                    Re: Is 'extern' necessary? David Brown <david.brown@hesbynett.no> - 2017-02-14 09:28 +0100
                      Re: Is 'extern' necessary? Robert Wessel <robertwessel2@yahoo.com> - 2017-02-14 03:31 -0600
                      Re: Is 'extern' necessary? BartC <bc@freeuk.com> - 2017-02-14 11:42 +0000
                        Re: Is 'extern' necessary? David Brown <david.brown@hesbynett.no> - 2017-02-14 13:29 +0100
                          Re: Is 'extern' necessary? supercat@casperkitty.com - 2017-02-14 05:16 -0800
                            Re: Is 'extern' necessary? David Brown <david.brown@hesbynett.no> - 2017-02-14 14:51 +0100
                              Re: Is 'extern' necessary? supercat@casperkitty.com - 2017-02-14 07:24 -0800
                                Re: Is 'extern' necessary? David Brown <david.brown@hesbynett.no> - 2017-02-14 21:35 +0100
                                  Re: Is 'extern' necessary? James Kuyper <jameskuyper@verizon.net> - 2017-02-14 15:53 -0500
                                    Re: Is 'extern' necessary? David Brown <david.brown@hesbynett.no> - 2017-02-15 09:31 +0100
                                      Re: Is 'extern' necessary? "James R. Kuyper" <jameskuyper@verizon.net> - 2017-02-15 11:36 -0500
                                        Re: Is 'extern' necessary? Keith Thompson <kst-u@mib.org> - 2017-02-15 09:12 -0800
                                          Re: Is 'extern' necessary? "James R. Kuyper" <jameskuyper@verizon.net> - 2017-02-15 12:58 -0500
                                        Re: Is 'extern' necessary? supercat@casperkitty.com - 2017-02-15 10:01 -0800
                                          Re: Is 'extern' necessary? jameskuyper@verizon.net - 2017-02-15 10:55 -0800
                                            Re: Is 'extern' necessary? supercat@casperkitty.com - 2017-02-15 11:42 -0800
                                              Re: Is 'extern' necessary? jameskuyper@verizon.net - 2017-02-15 12:28 -0800
                                                Re: Is 'extern' necessary? supercat@casperkitty.com - 2017-02-15 13:58 -0800
                            Re: Is 'extern' necessary? Keith Thompson <kst-u@mib.org> - 2017-02-14 08:54 -0800
                        Re: Is 'extern' necessary? Keith Thompson <kst-u@mib.org> - 2017-02-14 08:51 -0800
                          Re: Is 'extern' necessary? Richard Heathfield <rjh@cpax.org.uk> - 2017-02-14 17:32 +0000
                            Re: Is 'extern' necessary? Gareth Owen <gwowen@gmail.com> - 2017-02-14 17:45 +0000
                          Re: Is 'extern' necessary? David Brown <david.brown@hesbynett.no> - 2017-02-14 21:38 +0100
                          Re: Is 'extern' necessary? BartC <bc@freeuk.com> - 2017-02-14 23:26 +0000
                            Re: Is 'extern' necessary? Keith Thompson <kst-u@mib.org> - 2017-02-14 16:33 -0800
                              Re: Is 'extern' necessary? BartC <bc@freeuk.com> - 2017-02-15 00:47 +0000
                        Re: Is 'extern' necessary? Jean-Marc Bourguet <jm@bourguet.org> - 2017-02-14 18:17 +0100
                          Re: Is 'extern' necessary? Keith Thompson <kst-u@mib.org> - 2017-02-14 09:47 -0800
                            Re: Is 'extern' necessary? BartC <bc@freeuk.com> - 2017-02-14 18:36 +0000
                              Re: Is 'extern' necessary? jameskuyper@verizon.net - 2017-02-14 11:20 -0800
                              Re: Is 'extern' necessary? supercat@casperkitty.com - 2017-02-14 11:42 -0800
                                Re: Is 'extern' necessary? BartC <bc@freeuk.com> - 2017-02-14 20:22 +0000
                              Re: Is 'extern' necessary? Robert Wessel <robertwessel2@yahoo.com> - 2017-02-14 17:58 -0600
                                Re: Is 'extern' necessary? BartC <bc@freeuk.com> - 2017-02-15 01:29 +0000
                                  Re: Is 'extern' necessary? Robert Wessel <robertwessel2@yahoo.com> - 2017-02-16 05:02 -0600
                                    Re: Is 'extern' necessary? BartC <bc@freeuk.com> - 2017-02-16 11:38 +0000
                                      Re: Is 'extern' necessary? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-16 14:37 +0000
                                        Re: Is 'extern' necessary? BartC <bc@freeuk.com> - 2017-02-16 15:43 +0000
                                          Re: Is 'extern' necessary? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-16 16:08 +0000
                                      Re: Is 'extern' necessary? GOTHIER Nathan <nathan.gothier@gmail.com> - 2017-02-16 18:42 +0100
                                Re: Is 'extern' necessary? David Brown <david.brown@hesbynett.no> - 2017-02-15 09:47 +0100
                                  Re: Is 'extern' necessary? Robert Wessel <robertwessel2@yahoo.com> - 2017-02-16 05:05 -0600
            Re: Is 'extern' necessary? Tim Rentsch <txr@alumni.caltech.edu> - 2017-02-15 16:53 -0800
      Re: Is 'extern' necessary? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-02-10 23:55 +0000
    Re: Is 'extern' necessary? Les Cargill <lcargill99@comcast.com> - 2017-02-10 19:24 -0600
    Re: Is 'extern' necessary? void <nathan.gothier@gmail.com> - 2017-02-11 03:17 -0800

Page 1 of 6  [1] 2 3 4 5 6  Next page →


#103611 — Is 'extern' necessary?

FromBartC <bc@freeuk.com>
Date2017-02-10 17:16 +0000
SubjectIs 'extern' necessary?
Message-ID<o7ksei$n0$1@dont-email.me>
I've been trying to find out what the point of 'extern' is.

Usually, if you declare:

  int abc;

at filespec in modules A and B, then you end up with one copy of 'abc' 
shared across both modules. It doesn't matter which of A or B is abc's 
'home' module.

If one of them uses 'extern', then it's the same. But it fails when both 
use 'extern'.

Unless one (or both) modules has a second declaration with just 'int 
abc;'; that seems to have the effect of cancelling 'extern'. So it must 
be doing something, but what?

When abc is initialised, then this is fine too (unless you initialise 
the declaration with 'extern'), but now only one module can initialise 
it; initialising both causes link errors.

It seems that most of the time you can get by without 'extern'. So when 
is it necessary?

-- 
Bartc

[toc] | [next] | [standalone]


#103613

FromKeith Thompson <kst-u@mib.org>
Date2017-02-10 09:33 -0800
Message-ID<lnd1eqvtl6.fsf@kst-u.example.com>
In reply to#103611
BartC <bc@freeuk.com> writes:
> I've been trying to find out what the point of 'extern' is.

N1570 6.9.2, tentative definitions.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#103617

FromBartC <bc@freeuk.com>
Date2017-02-10 18:41 +0000
Message-ID<o7l1ci$l0t$1@dont-email.me>
In reply to#103613
On 10/02/2017 17:33, Keith Thompson wrote:
> BartC <bc@freeuk.com> writes:
>> I've been trying to find out what the point of 'extern' is.
>
> N1570 6.9.2, tentative definitions.

It mentions 'external definition'. What does that mean?

Presumably it can't mean a definition with 'extern', because if I write:

   extern int abc = 100;

then gcc complains (but splitting it into extern int abc; int abc=100; 
is OK).

In my mind, file-scope objects can be declared in a module as either 
Local, Exported or Imported. Only the first two can be initialised in 
the module (initialising of an Imported object is up to the module that 
exports it).

I understand that in C, separate declarations that may appear 
conflicting are necessary, in order to have a shared declaration of X 
that can appear, via a shared header, in both modules A and B, where 
there is also a second, perhaps initialised definition of X in one of 
those modules.

But with this extern, tentative, storage and linkage business, it's 
doing a good job of making it as fiendishly convoluted as possible. 
Having issues hidden by a compiler, or multiple definitions taken care 
of by a linker, doesn't help in trying to understand.

-- 
Bartc



[toc] | [prev] | [next] | [standalone]


#103618

FromKeith Thompson <kst-u@mib.org>
Date2017-02-10 12:09 -0800
Message-ID<ln60khx0y2.fsf@kst-u.example.com>
In reply to#103617
BartC <bc@freeuk.com> writes:
> On 10/02/2017 17:33, Keith Thompson wrote:
>> BartC <bc@freeuk.com> writes:
>>> I've been trying to find out what the point of 'extern' is.
>>
>> N1570 6.9.2, tentative definitions.
>
> It mentions 'external definition'. What does that mean?

The title of section 6.9 is "External definitions".  The definition of
the term is in paragraph 5.

[...]

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#103622

FromBartC <bc@freeuk.com>
Date2017-02-10 20:29 +0000
Message-ID<o7l7no$eu4$1@dont-email.me>
In reply to#103618
On 10/02/2017 20:09, Keith Thompson wrote:
> BartC <bc@freeuk.com> writes:
>> On 10/02/2017 17:33, Keith Thompson wrote:
>>> BartC <bc@freeuk.com> writes:
>>>> I've been trying to find out what the point of 'extern' is.
>>>
>>> N1570 6.9.2, tentative definitions.
>>
>> It mentions 'external definition'. What does that mean?
>
> The title of section 6.9 is "External definitions".  The definition of
> the term is in paragraph 5.

That defines it in terms of 'external declaration'!

All I'm to trying to establish is whether an external /whatever/ is just 
a /whatever/ with an 'extern' at the start. If so, then why is it that:

    extern int abc = 100;

gives me this message in gcc (-Wpedantic-errors):

    warning: 'abc' initialized and declared 'extern'

That suggests that something is not quite right.

And the mystery doesn't end there, as I can't figure out why the 
following is allowed:

    static int abc = 100;
    extern int abc;

(Here, 'extern' seems to be ignored) But this isn't:

    extern int abc;
    static int abc = 100;

I guess, because the Standard says so!


-- 
Bartc

[toc] | [prev] | [next] | [standalone]


#103628

FromKeith Thompson <kst-u@mib.org>
Date2017-02-10 13:26 -0800
Message-ID<lnwpcxvisr.fsf@kst-u.example.com>
In reply to#103622
BartC <bc@freeuk.com> writes:
> On 10/02/2017 20:09, Keith Thompson wrote:
>> BartC <bc@freeuk.com> writes:
>>> On 10/02/2017 17:33, Keith Thompson wrote:
>>>> BartC <bc@freeuk.com> writes:
>>>>> I've been trying to find out what the point of 'extern' is.
>>>>
>>>> N1570 6.9.2, tentative definitions.
>>>
>>> It mentions 'external definition'. What does that mean?
>>
>> The title of section 6.9 is "External definitions".  The definition of
>> the term is in paragraph 5.
>
> That defines it in terms of 'external declaration'!

Paragraph 4.

Does your PDF reader not have a search function?

[...]

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#103637

FromBartC <bc@freeuk.com>
Date2017-02-10 22:56 +0000
Message-ID<o7lgcf$e16$1@dont-email.me>
In reply to#103628
On 10/02/2017 21:26, Keith Thompson wrote:
> BartC <bc@freeuk.com> writes:
>> On 10/02/2017 20:09, Keith Thompson wrote:
>>> BartC <bc@freeuk.com> writes:
>>>> On 10/02/2017 17:33, Keith Thompson wrote:
>>>>> BartC <bc@freeuk.com> writes:
>>>>>> I've been trying to find out what the point of 'extern' is.
>>>>>
>>>>> N1570 6.9.2, tentative definitions.
>>>>
>>>> It mentions 'external definition'. What does that mean?
>>>
>>> The title of section 6.9 is "External definitions".  The definition of
>>> the term is in paragraph 5.
>>
>> That defines it in terms of 'external declaration'!
>
> Paragraph 4.
>
> Does your PDF reader not have a search function?

I'm sure it must be easier to just answer my original question than just 
keep on posting clues in some treasure hunt that doesn't appear to lead 
anywhere!

Looking for the whole word 'extern' gives me 44 instances of which the 
15th, in a comment for example code, contains the phrase: "... file 
scope declaration with extern creates an external definition.". Well, 
that's cleared up one point then. Although a simple Yes or No would have 
done too.

But that was about external declarations which lead on from external 
definitions which lead on from tentative definitions which lead on from 
my question, which was the point of using extern.

At the minute, I still haven't the foggiest what a tentative definition 
is when it's at home. The Standard says this:

"A declaration of an identifier for an object that has file scope 
without an initializer, and without a storage-class specifier or with 
the storage-class specifier static, constitutes a tentative definition."

Which is as clear as mud.

However it doesn't matter for now because I know enough of how it 
necessarily has to function, and how it's used in practice, to implement 
something that works (in being able to parse code and deduce what the 
intended linkage of anything is). That can be tweaked later as and when 
needed.

(Perhaps some might like to test their skills at working out what these 
definitions are supposed to mean. Take this selection:

   int A[];
   int A[3];
   int A[3]={10,20,30};
   extern int A[];
   extern int A[3];
   extern int A[3]={10,20,30};
   static int A[];
   static int A[3];
   static int A[3]={10,20,30};

Choose one at random, and work out whether it's a Local, Exported or 
Imported object. Then choose another at random (it could be the same 
one), and see how that combines with the first: whether it's legal or 
how the attribute of A changes. And you can continue the game with 
further selections (the same game C compilers have to play).

Perhaps try it with the first declaration at file-scope, and the second 
in a block scope, or both in the same block scope.

Now you may want to compare how C works with a certain other language 
with the same aims. There, A can only be declared in its home module and 
in one of these two forms:

         [3]int A = (10,20,30)   # Defined locally, not exported

  global [3]int A = (10,20,30)   # Defined locally, exported

Any redeclarations are illegal. No explicit declaration of A is 
necessary in a module that imports this one.

See how simple it can be? I've used several schemes all simpler than C's.)

-- 
Bartc

[toc] | [prev] | [next] | [standalone]


#103656

Fromjameskuyper@verizon.net
Date2017-02-10 20:19 -0800
Message-ID<258c42f1-d13a-48c2-8a8d-558587382ecb@googlegroups.com>
In reply to#103637
On Friday, February 10, 2017 at 5:57:06 PM UTC-5, Bart wrote:
> On 10/02/2017 21:26, Keith Thompson wrote:
> > BartC <bc@freeuk.com> writes:
> >> On 10/02/2017 20:09, Keith Thompson wrote:
...
> >>> The title of section 6.9 is "External definitions".  The definition of
> >>> the term is in paragraph 5.
> >>
> >> That defines it in terms of 'external declaration'!
> >
> > Paragraph 4.
> >
> > Does your PDF reader not have a search function?
> 
> I'm sure it must be easier to just answer my original question than just 
> keep on posting clues in some treasure hunt that doesn't appear to lead 
> anywhere!

External definitions are defined in 6.9p1:

"external-declaration:
        function-definition
        declaration"

However, there's a subtle issue that isn't entirely obvious from that grammar
rule alone. There's only one grammar rule that uses "external-declaration", and
it's the rule that immediately precedes it in 6.9p1:

"translation-unit:
         external-declaration
         translation-unit external-declaration"

The significance of this fact is that a declaration can be parsed as an
external-declaration only if it is a top-level part of a translation-unit. If
one declaration occurs inside another declaration, it is an external
declaration, not an external one. Only declarations that aren't inside anything
else qualify as external declarations.

Keith directed you to 6.9p4, which says the same thing less precisely, using
ordinary English words instead of grammar rules: "... a translation unit ...
consists of a sequence of external declarations. These are described as
‘‘external’’ because they appear outside any function (and hence have file
scope)."
Note that translation units do not appear until after preprocessing is complete;
until that point has been reached in the translation process, what you have is a
preprocessing translation unit, and 6.9 isn't talking about those. All
preprocessing directives, comments, and other white space have all been removed
by that point. The only things left are declarations of user-defined types,
declarations of typedefs, declarations of object identifiers (some of which are
also definitions of those identifiers), declarations of function identifiers,
and definitions of functions. All of those are external declarations. Those
object and function definitions are external definitions.

> Looking for the whole word 'extern' gives me 44 instances of which the 

Why didn't you search for "external declaration"? The first several hits with
that expression occur in 6.9, immediately following the place were external
declaration is defined. It's annoying that the '-' prevents that definition
itself from coming up in the search results. Hint for future reference: if you
think that something might correspond to an element of the C grammar, replace
the spaces with '-' while searching for it.

> 15th, in a comment for example code, contains the phrase: "... file 
> scope declaration with extern creates an external definition.". Well, 
> that's cleared up one point then. Although a simple Yes or No would have 
> done too.

That's in the section describing the "inline" function specifiers. What it says
is true only in that context, where the "extern" on the second declaration of
fahr() causes an external definition to be created from the previously provided 
inline definition. It is not true in general. Neither of the following would be,
in itself, an external definition, even if they occurred at file scope:

    extern int eint;
    extern void func(void);

Both of those are declarations, neither of them is a definition.

[toc] | [prev] | [next] | [standalone]


#103677

From"James R. Kuyper" <jameskuyper@verizon.net>
Date2017-02-11 09:53 -0500
Message-ID<5244d87a-3f9d-4b32-a605-fd5f2cb8340b@verizon.net>
In reply to#103656
On 02/10/2017 11:19 PM, jameskuyper@verizon.net wrote:
...
> external-declaration only if it is a top-level part of a translation-unit. If
> one declaration occurs inside another declaration, it is an external
> declaration, not an external one. Only declarations that aren't inside anything

Sorry - one typo converted that into complete nonsense. That should have 
been:
"... it is an internal declaration, not an external one ..."

[toc] | [prev] | [next] | [standalone]


#103796

FromTim Rentsch <txr@alumni.caltech.edu>
Date2017-02-13 05:19 -0800
Message-ID<kfny3xa1b4p.fsf@x-alumni2.alumni.caltech.edu>
In reply to#103637
BartC <bc@freeuk.com> writes:

> On 10/02/2017 21:26, Keith Thompson wrote:
>> BartC <bc@freeuk.com> writes:
>>> On 10/02/2017 20:09, Keith Thompson wrote:
>>>> BartC <bc@freeuk.com> writes:
>>>>> On 10/02/2017 17:33, Keith Thompson wrote:
>>>>>> BartC <bc@freeuk.com> writes:
>>>>>>> I've been trying to find out what the point of 'extern' is.
>>>>>>
>>>>>> N1570 6.9.2, tentative definitions.
>>>>>
>>>>> It mentions 'external definition'.  What does that mean?
>>>>
>>>> The title of section 6.9 is "External definitions".  The
>>>> definition of the term is in paragraph 5.
>>>
>>> That defines it in terms of 'external declaration'!
>>
>> Paragraph 4.
>>
>> Does your PDF reader not have a search function?
>
> I'm sure it must be easier to just answer my original question than
> just keep on posting clues in some treasure hunt that doesn't appear
> to lead anywhere!

Of course I'm only guessing at Keith's motives, but I suspect his
goal is not to answer your question but to help you learn to use
a tool that allows you to find most of the answers to such
questions by yourself.  It's the old "give a man a fish, teach a
man to fish" thing.  So which would you rather do:  learn how to
fish, or complain all the time about being hungry?  The choice is
really up to you.

[toc] | [prev] | [next] | [standalone]


#103619

Fromjameskuyper@verizon.net
Date2017-02-10 12:09 -0800
Message-ID<4920b63e-c9ac-4ddb-96f4-5c45257f6689@googlegroups.com>
In reply to#103617
On Friday, February 10, 2017 at 1:41:10 PM UTC-5, Bart wrote:
> On 10/02/2017 17:33, Keith Thompson wrote:
> > BartC <bc@freeuk.com> writes:
> >> I've been trying to find out what the point of 'extern' is.
> >
> > N1570 6.9.2, tentative definitions.
> 
> It mentions 'external definition'. What does that mean?

Go up one level. Section 6.9 is titled "External definitions", 6.9p1 defines what an external declaration is, and 6.9p5 defines an external definition as an external declaration that is also a definition, referring back to 6.7p5 for the definition of "definition". 

> Presumably it can't mean a definition with 'extern', because if I write:
> 
>    extern int abc = 100;

In 6.9, "external" means "outside of any function" (6.9p4), whereas in most other parts of the standard it refers to "external linkage".
 
> then gcc complains (but splitting it into extern int abc; int abc=100; 
> is OK).

It's only a warning, even with -pedantic-errors chosen. The warning is because that doesn't match the conventional use of extern: to declare an object without defining it.

> In my mind, file-scope objects can be declared in a module as either 
> Local, Exported or Imported. Only the first two can be initialised in 
> the module (initialising of an Imported object is up to the module that 
> exports it).

The C standard doesn't care much about how you conceptualize things, and the way you've chosen to conceptualize things often seems to impede your ability to understand the different ways used by C. Still, there doesn't seem to be a big difference between your concept and the way C does things. You make a file scope object Local by defining it with the "static" keyword. You Import them by declaring them with the extern keyword. You Export them objects by defining them with neither of those keywords. C allows other more complicated combinations, but it supports the concepts you describe. 

The biggest difference between your concept and C's appears to be that:

> I understand that in C, separate declarations that may appear 
> conflicting are necessary, in order to have a shared declaration of X 
> that can appear, via a shared header, in both modules A and B, where 
> there is also a second, perhaps initialised definition of X in one of

No, there can't be a second definition. If there's a definition, there's only one. There can be a tentative definition, but if in the same TU as an actual definition, the tentative definition does NOT become an actual definition, which is what makes it "tentative". 

> those modules.
> 
> But with this extern, tentative, storage and linkage business, it's 
> doing a good job of making it as fiendishly convoluted as possible. 
> Having issues hidden by a compiler, or multiple definitions taken care 
> of by a linker, doesn't help in trying to understand.

It makes things easier for the developer, however. Each of those rules enables idioms that C programmers other than you find convenient.

[toc] | [prev] | [next] | [standalone]


#103624

FromBartC <bc@freeuk.com>
Date2017-02-10 20:50 +0000
Message-ID<o7l8uh$jip$1@dont-email.me>
In reply to#103619
On 10/02/2017 20:09, jameskuyper@verizon.net wrote:
> On Friday, February 10, 2017 at 1:41:10 PM UTC-5, Bart wrote:
>> On 10/02/2017 17:33, Keith Thompson wrote:
>>> BartC <bc@freeuk.com> writes:
>>>> I've been trying to find out what the point of 'extern' is.
>>>
>>> N1570 6.9.2, tentative definitions.
>>
>> It mentions 'external definition'. What does that mean?
>
> Go up one level. Section 6.9 is titled "External definitions", 6.9p1 defines what an external declaration is, and 6.9p5 defines an external definition as an external declaration that is also a definition, referring back to 6.7p5 for the definition of "definition".
>
>> Presumably it can't mean a definition with 'extern', because if I write:
>>
>>    extern int abc = 100;
>
> In 6.9, "external" means "outside of any function" (6.9p4), whereas in most other parts of the standard it refers to "external linkage".

I remember that from a previous discussion. It comes up in K&R2 1.10 
(p31): "An external variable... must be defined outside of any function. 
...The variable must also be declared in each function that wants to 
access it...". And it goes on to suggest using an explicit 'extern' 
statement.

I think that's just ramped up the confusion by a magnitude or so!

>> But with this extern, tentative, storage and linkage business, it's
>> doing a good job of making it as fiendishly convoluted as possible.
>> Having issues hidden by a compiler, or multiple definitions taken care
>> of by a linker, doesn't help in trying to understand.
>
> It makes things easier for the developer, however. Each of those rules enables idioms that C programmers other than you find convenient.

What sort of idioms?

I can see a couple of ways to share exported data in C. If module A 
wants to share a named object with B:

   A.c:                        B.c:

   extern int abc;             extern int abc;   #include A.h
   ....
   int abc = 100;

But it also seems to work without extern:

   A.c:                        B.c:

   int abc;                    int abc;          #include A.h
   ....
   int abc = 100;

Here, B doesn't know if abc belongs to B, or is imported from elsewhere. 
A can assume that abc does belong because it's initialised. So object 
files for both A and B appear to export abc, so only one is chosen by 
the linker (since A contains init data for abc, that can be chosen).

The same thing applies when abc is a function. and the abc = 100 is 
replaced a function body, except that now B /knows/ that abc has to 
exist elsewhere. There is a link error if both A and B export abc with 
init data or a function body.

So, why is any other idiom needed? The usage pattern required is simple.

-- 
Bartc

[toc] | [prev] | [next] | [standalone]


#103625

Fromsupercat@casperkitty.com
Date2017-02-10 13:09 -0800
Message-ID<39f56fcf-9192-48cb-8474-caeddff1608a@googlegroups.com>
In reply to#103624
On Friday, February 10, 2017 at 2:50:11 PM UTC-6, Bart wrote:
> I can see a couple of ways to share exported data in C. If module A 
> wants to share a named object with B:
> 
>    A.c:                        B.c:
> 
>    extern int abc;             extern int abc;   #include A.h
>    ....
>    int abc = 100;
> 
> But it also seems to work without extern:
> 
>    A.c:                        B.c:
> 
>    int abc;                    int abc;          #include A.h
>    ....
>    int abc = 100;

> So, why is any other idiom needed? The usage pattern required is simple.

C implementations exist on platforms with many kinds of linkers.  Some
of them require every symbol that is shared between modules be exported
by exactly one module (with the rest of the models importing it).  Others
can allow multiple modules to weakly export a symbol provided that at least
one exports the symbol somehow, and at most one exports it strongly.  Code
which includes non-qualified declarations of the same name in different
files will thus be accepted by some linkers and rejected by others.

[toc] | [prev] | [next] | [standalone]


#103629

FromKeith Thompson <kst-u@mib.org>
Date2017-02-10 13:33 -0800
Message-ID<lnshnlvih7.fsf@kst-u.example.com>
In reply to#103624
BartC <bc@freeuk.com> writes:
> On 10/02/2017 20:09, jameskuyper@verizon.net wrote:
>> On Friday, February 10, 2017 at 1:41:10 PM UTC-5, Bart wrote:
>>> On 10/02/2017 17:33, Keith Thompson wrote:
>>>> BartC <bc@freeuk.com> writes:
>>>>> I've been trying to find out what the point of 'extern' is.
>>>>
>>>> N1570 6.9.2, tentative definitions.
>>>
>>> It mentions 'external definition'. What does that mean?
>>
>> Go up one level. Section 6.9 is titled "External definitions", 6.9p1
>> defines what an external declaration is, and 6.9p5 defines an
>> external definition as an external declaration that is also a
>> definition, referring back to 6.7p5 for the definition of
>> "definition".
>>
>>> Presumably it can't mean a definition with 'extern', because if I write:
>>>
>>>    extern int abc = 100;
>>
>> In 6.9, "external" means "outside of any function" (6.9p4), whereas
>> in most other parts of the standard it refers to "external linkage".
>
> I remember that from a previous discussion. It comes up in K&R2 1.10 
> (p31): "An external variable... must be defined outside of any function. 
> ...The variable must also be declared in each function that wants to 
> access it...". And it goes on to suggest using an explicit 'extern' 
> statement.
>
> I think that's just ramped up the confusion by a magnitude or so!

That appears to be some unfortunate wording in K&R2.  It implies (and
the example shows) that the variables need to be declared *inside* each
function that refers to them.  In fact, the declaration merely needs to
be visible.

Note that the idea of header files has not yet been introduced at that
point.

[...]

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#103642

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2017-02-10 23:23 +0000
Message-ID<87shnlzl3t.fsf@bsb.me.uk>
In reply to#103624
BartC <bc@freeuk.com> writes:
<snip>
> I can see a couple of ways to share exported data in C. If module A
> wants to share a named object with B:
>
>   A.c:                        B.c:
>
>   extern int abc;             extern int abc;   #include A.h
>   ....
>   int abc = 100;

Yes, that's the usual idiom.

> But it also seems to work without extern:
>
>   A.c:                        B.c:
>
>   int abc;                    int abc;          #include A.h
>   ....
>   int abc = 100;

That's undefined.  For example tcc objects to this and it is perfectly
with its rights to do so.  gcc can be asked to complain about this (use
-fno-common) but it does not have to complain in order to be
conforming.  This came up a year ago.

<snip>
> So, why is any other idiom needed? The usage pattern required is
> simple.

There's only one idiom in current use.  I think some authors of very old
C often assumed that common definitions would be merged, but that was
never guaranteed, nor was it ever make standard.

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#103645

FromBartC <bc@freeuk.com>
Date2017-02-10 23:35 +0000
Message-ID<o7lil2$nt1$1@dont-email.me>
In reply to#103642
On 10/02/2017 23:23, Ben Bacarisse wrote:
> BartC <bc@freeuk.com> writes:

>> But it also seems to work without extern:
>>
>>   A.c:                        B.c:
>>
>>   int abc;                    int abc;          #include A.h
>>   ....
>>   int abc = 100;
>
> That's undefined.  For example tcc objects to this and it is perfectly
> with its rights to do so.

Yes, it does. And I could choose to do the same (and make things simpler 
for me).

However ... it works with five other compilers so seems to be outvoted!

I think that if it is commonly expected to work, then it might need to 
do so.

-- 
Bartc

[toc] | [prev] | [next] | [standalone]


#103652

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2017-02-11 00:34 +0000
Message-ID<87y3xdy39e.fsf@bsb.me.uk>
In reply to#103645
BartC <bc@freeuk.com> writes:

> On 10/02/2017 23:23, Ben Bacarisse wrote:
>> BartC <bc@freeuk.com> writes:
>
>>> But it also seems to work without extern:
>>>
>>>   A.c:                        B.c:
>>>
>>>   int abc;                    int abc;          #include A.h
>>>   ....
>>>   int abc = 100;
>>
>> That's undefined.  For example tcc objects to this and it is perfectly
>> with its rights to do so.
>
> Yes, it does. And I could choose to do the same (and make things
> simpler for me).

And it works with tcc -fcommon (on my machine)

> However ... it works with five other compilers so seems to be
> outvoted!

As I said, it fails with gcc -fno-common (on my machine) and I'd hope
other compilers offer something similar.  It's not really a vote based
on compilers but on other features of the system.

> I think that if it is commonly expected to work, then it might need to
> do so.

Ah, you may need to do option polls for that.

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#103788

FromDavid Brown <david.brown@hesbynett.no>
Date2017-02-13 12:41 +0100
Message-ID<o7s5uq$lfc$1@dont-email.me>
In reply to#103642
On 11/02/17 00:23, Ben Bacarisse wrote:
> BartC <bc@freeuk.com> writes:
> <snip>
>> I can see a couple of ways to share exported data in C. If module A
>> wants to share a named object with B:
>>
>>   A.c:                        B.c:
>>
>>   extern int abc;             extern int abc;   #include A.h
>>   ....
>>   int abc = 100;
> 
> Yes, that's the usual idiom.
> 
>> But it also seems to work without extern:
>>
>>   A.c:                        B.c:
>>
>>   int abc;                    int abc;          #include A.h
>>   ....
>>   int abc = 100;
> 
> That's undefined.  For example tcc objects to this and it is perfectly
> with its rights to do so.  gcc can be asked to complain about this (use
> -fno-common) but it does not have to complain in order to be
> conforming.  This came up a year ago.
> 
> <snip>
>> So, why is any other idiom needed? The usage pattern required is
>> simple.
> 
> There's only one idiom in current use.  I think some authors of very old
> C often assumed that common definitions would be merged, but that was
> never guaranteed, nor was it ever make standard.
> 

Also note that when you use a "common" section with symbol merging, you
usually have little or no control or even warning messages if you happen
to have "int abc;" in A.c and "double abc;" in B.c.  The linker might
merge these together and give you a single 8-bytes space (assuming
typical sizes here) called "abc".  One or more files are interpreting it
as an "int", while one or more other files are interpreting the space as
a "double" - you program is screwed.  And the linker might equally well
merge them together as a single 4-byte space (it can pick either symbol
as the definition), thus screwing your program in even more ways.

[toc] | [prev] | [next] | [standalone]


#103794

FromBartC <bc@freeuk.com>
Date2017-02-13 13:14 +0000
Message-ID<o7sbc9$83h$1@dont-email.me>
In reply to#103788
On 13/02/2017 11:41, David Brown wrote:

> Also note that when you use a "common" section with symbol merging, you
> usually have little or no control or even warning messages if you happen
> to have "int abc;" in A.c and "double abc;" in B.c.  The linker might
> merge these together and give you a single 8-bytes space (assuming
> typical sizes here) called "abc".  One or more files are interpreting it
> as an "int", while one or more other files are interpreting the space as
> a "double" - you program is screwed.  And the linker might equally well
> merge them together as a single 4-byte space (it can pick either symbol
> as the definition), thus screwing your program in even more ways.

Is a linker even an official part of the language? It does seem to be a 
necessary component of most implementations.

The compiler project I'm having a go at won't use a linker. It will be 
able to 'see' all global declarations across modules and so detect any 
type mismatches**. Actually lots of interesting things become possible.

It's odd that many compilers like gcc can be given multiple modules that 
will be linked to form one executable, yet they cannot detect disparate 
types of global variables even though all the information is there. 
Maybe they reserve that for 'link-time optimisation' although that's 
leaving it a bit late IMO.

(** When all sources are available. It won't work when some code is in a 
dynamic library, but then variables aren't usually exported from dynamic 
libraries; it's usually functions. It will then depend on how accurate 
and up-to-date the associated header file is in correctly describing 
types and interfaces.

You sometimes see 'decorated' function names that incorporate extra 
information, but again that's a link-time scheme.)

-- 
Bartc

[toc] | [prev] | [next] | [standalone]


#103801

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2017-02-13 14:05 +0000
Message-ID<87lgtatccg.fsf@bsb.me.uk>
In reply to#103794
BartC <bc@freeuk.com> writes:

> On 13/02/2017 11:41, David Brown wrote:
>
>> Also note that when you use a "common" section with symbol merging, you
>> usually have little or no control or even warning messages if you happen
>> to have "int abc;" in A.c and "double abc;" in B.c.  The linker might
>> merge these together and give you a single 8-bytes space (assuming
>> typical sizes here) called "abc".  One or more files are interpreting it
>> as an "int", while one or more other files are interpreting the space as
>> a "double" - you program is screwed.  And the linker might equally well
>> merge them together as a single 4-byte space (it can pick either symbol
>> as the definition), thus screwing your program in even more ways.
>
> Is a linker even an official part of the language? It does seem to be
> a necessary component of most implementations.

The language is just symbols in sequence.  An implementation of the
language must give those symbol the correct execution semantics as
defined in the standard.  So the implementation is all the pieces that
are needed to run programs.  That can give very odd effects when the
linker is from one vendor, the library from another and the compiler
from a third.

So, no, you certainly don't need a linker.

<snip>
-- 
Ben.

[toc] | [prev] | [next] | [standalone]


Page 1 of 6  [1] 2 3 4 5 6  Next page →

Back to top | Article view | comp.lang.c


csiph-web