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


Groups > comp.lang.c > #111669

Re: Style question: Declaration of globals in header file

From Andrey Tarasevich <andreytarasevich@hotmail.com>
Newsgroups comp.lang.c
Subject Re: Style question: Declaration of globals in header file
Date 2017-06-06 18:37 -0700
Organization A noiseless patient Spider
Message-ID <oh7l5m$ble$1@dont-email.me> (permalink)
References <oh6p0c$e9k$1@gioia.aioe.org>

Show all headers | View raw


On 6/6/2017 10:33 AM, Steve Keller wrote:
> The C standard allows the
> declarations in the header file to be with or without the extern
> storage class specifier, i.e. one could write
>
>     -- foo.h --
>     int foo;
>     int bar;
>     -- foo.c --
>     #include "foo.h"
>
>     int bar = 1;
>     ...
>     -----------

Nope. C standard does not allow that, assuming that you include this 
header file into multiple translation units.

This approach to global declarations is specific to "Relaxed Ref/Def" 
model (per "C99 Rationale V5.10" nomenclature), which was popular in 
native UNIX compilers when C was first standardized. However, this 
approach was rejected by the committee/WG in favor of mixed "Strict 
Ref/Def"+"Initialization" model (same nomenclature). Standard model does 
not allow multiple definitions of the same external object, even if 
these definitions do not include an initializer.

You can read more about the history of that matter "6.2.2 Linkages of 
identifiers" of publicly available "C99 Rationale V5.10" document.

If your implementation accepts the above (again, assuming that you 
include this header file into multiple translation units), it probably 
means that it gravitates towards historical UNIX ref/def model and 
implements it as an extension.

> or
>
>     -- foo.h --
>     extern int foo;
>     extern int bar;
>     -- foo.c --
>     #include "foo.h"
>
>     int foo;
>     int bar = 1;
>     ...
>     -----------

This, on the other hand, is the proper way to do it under standard model.

> So, what do you usually use in such situations?

I prefer to write my code in standard C, unless I have a very very very 
good reason to do otherwise.

-- 
Best regards,
Andrey Tarasevich

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Style question: Declaration of globals in header file Steve Keller <keller@no.invalid> - 2017-06-06 19:33 +0200
  Re: Style question: Declaration of globals in header file "James R. Kuyper" <jameskuyper@verizon.net> - 2017-06-06 14:21 -0400
    Re: Style question: Declaration of globals in header file Steve Keller <keller@no.invalid> - 2017-06-12 11:07 +0200
      Re: Style question: Declaration of globals in header file bartc <bc@freeuk.com> - 2017-06-12 11:03 +0100
      Re: Style question: Declaration of globals in header file Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-06-12 11:40 +0100
      Re: Style question: Declaration of globals in header file James Kuyper <jameskuyper@verizon.net> - 2017-06-12 07:33 -0400
        Re: Style question: Declaration of globals in header file Steve Keller <keller@no.invalid> - 2017-06-12 18:00 +0200
          Re: Style question: Declaration of globals in header file "James R. Kuyper" <jameskuyper@verizon.net> - 2017-06-12 12:11 -0400
            Re: Style question: Declaration of globals in header file Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-06-12 17:27 +0100
          Re: Style question: Declaration of globals in header file Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-06-12 17:18 +0100
          Re: Style question: Declaration of globals in header file supercat@casperkitty.com - 2017-06-12 09:44 -0700
      Re: Style question: Declaration of globals in header file supercat@casperkitty.com - 2017-06-12 07:55 -0700
        Re: Style question: Declaration of globals in header file Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-06-12 16:51 +0100
  Re: Style question: Declaration of globals in header file Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-06-06 19:21 +0100
  Re: Style question: Declaration of globals in header file Jorgen Grahn <grahn+nntp@snipabacken.se> - 2017-06-06 19:54 +0000
  Re: Style question: Declaration of globals in header file Andrey Tarasevich <andreytarasevich@hotmail.com> - 2017-06-06 18:37 -0700
    Re: Style question: Declaration of globals in header file supercat@casperkitty.com - 2017-06-07 07:54 -0700
      Re: Style question: Declaration of globals in header file David Brown <david.brown@hesbynett.no> - 2017-06-07 17:23 +0200
        Re: Style question: Declaration of globals in header file supercat@casperkitty.com - 2017-06-07 11:10 -0700
          Re: Style question: Declaration of globals in header file David Brown <david.brown@hesbynett.no> - 2017-06-07 22:13 +0200
            Re: Style question: Declaration of globals in header file supercat@casperkitty.com - 2017-06-07 14:57 -0700
              Re: Style question: Declaration of globals in header file David Brown <david.brown@hesbynett.no> - 2017-06-08 00:05 +0200
              Re: Style question: Declaration of globals in header file bartc <bc@freeuk.com> - 2017-06-07 23:19 +0100
                Re: Style question: Declaration of globals in header file supercat@casperkitty.com - 2017-06-07 16:16 -0700
              Re: Style question: Declaration of globals in header file Ian Collins <ian-news@hotmail.com> - 2017-06-08 12:56 +1200
                Re: Style question: Declaration of globals in header file David Brown <david.brown@hesbynett.no> - 2017-06-08 08:51 +0200
                Re: Style question: Declaration of globals in header file David Thompson <dave.thompson2@verizon.net> - 2017-07-09 22:43 -0400
                Re: Style question: Declaration of globals in header file supercat@casperkitty.com - 2017-07-10 08:46 -0700
                Re: Style question: Declaration of globals in header file Robert Wessel <robertwessel2@yahoo.com> - 2017-07-10 21:00 -0500
                Re: Style question: Declaration of globals in header file supercat@casperkitty.com - 2017-07-10 21:08 -0700
                Re: Style question: Declaration of globals in header file Robert Wessel <robertwessel2@yahoo.com> - 2017-07-11 01:18 -0500
                Re: Style question: Declaration of globals in header file bartc <bc@freeuk.com> - 2017-06-08 10:10 +0100
                Re: Style question: Declaration of globals in header file raltbos@xs4all.nl (Richard Bos) - 2017-06-17 18:08 +0000
            Re: Style question: Declaration of globals in header file "Patrick.Schluter" <Patrick.Schluter@free.fr> - 2017-06-10 08:36 +0200
    Re: Style question: Declaration of globals in header file Tim Rentsch <txr@alumni.caltech.edu> - 2017-06-09 10:09 -0700
      Re: Style question: Declaration of globals in header file supercat@casperkitty.com - 2017-06-09 12:17 -0700
  Re: Style question: Declaration of globals in header file Tim Rentsch <txr@alumni.caltech.edu> - 2017-06-09 09:52 -0700

csiph-web