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


Groups > comp.lang.c > #111753

Re: Style question: Declaration of globals in header file

From David Brown <david.brown@hesbynett.no>
Newsgroups comp.lang.c
Subject Re: Style question: Declaration of globals in header file
Date 2017-06-07 22:13 +0200
Organization A noiseless patient Spider
Message-ID <oh9miv$um2$1@dont-email.me> (permalink)
References <oh6p0c$e9k$1@gioia.aioe.org> <oh7l5m$ble$1@dont-email.me> <7b75c85b-8c3d-4716-aad4-ae452d719e95@googlegroups.com> <oh95jj$19i$1@dont-email.me> <8c067855-3441-4217-8465-06ec45778f60@googlegroups.com>

Show all headers | View raw


On 07/06/17 20:10, supercat@casperkitty.com wrote:
> On Wednesday, June 7, 2017 at 10:24:00 AM UTC-5, David Brown wrote:
>> On 07/06/17 16:54, supercat wrote:
>>> Another variation is:
>>>
>>> --foo.h
>>> #ifdef FOO_C
>>> #define varqual
>>> #else
>>> #define varqual extern
>>> #endif
>>> varqual int foo;
>>>
>>> --foo.c
>>> #define FOO_C
>>> #include "foo.h"
>>>
>>> This has the advantage of avoiding redundant declarations.
>>
>> This is, IMHO, a hideous way of exporting your variables.  I /really/
>> hate it when include files are not "stand alone" but depend on different
>> values of #define'd macros, ordering of #include files, number of times
>> included, etc.
> 
> The FOO_C macro would only be defined within FOO_C, and (while I forgot
> to mention it) the varqual macro should be undef'ed at the end, or else
> a module-specific name (FOO_VARQUAL) used instead.  The pattern avoids
> the need to have the source text contain two separate declarations for
> each variable that is shared between compilation units. 

If you think that is a problem, you have serious issues with your 
development practices.  Declare the exported symbol in the header, and 
define it in /one/ C file.  Make sure you include the header in the C 
file, and the compiler will tell you if they are out of synchronisation. 
  And if you are changing your global data so often that separate 
declarations and definitions are inconvenient, then your should scrap 
your program design and start again.

It is a /bad/ idea to make the same section of code have different 
effects at different times in the same program, due to pre-processor 
symbol definitions.  There are occasional uses, such as "x macros", but 
these should be used carefully and clearly.  It is far better if any 
given section of code in a header or a C file has the same meaning all 
the time in the given program build.

> Further, in cases
> where multiple modules will need to know the size of an exported table of
> constants, I would think it much safer to have both the size and the
> contents in the same (header) file, than to have the size in a header file
> but the contents in a separate source file.  

Nonsense.  How often might this be useful?  Once in a program?  Is that 
enough to spoil the rest of your code with a throwback from the nineties?

> Given, e.g.
> 
> foo.h
>      #define NUM_WIDGETS 4
>      extern unsigned widgets[NUM_WIDGETS];
> foo.c
>      #include <foo.h>
>      unsigned widgets[NUM_WIDGETS] = {1,2,3,4};
> 
> there would be no validation to ensure that foo.h and foo.c remain in sync.

First, you easily get validation against having too many initialisers. 
Checking against too few is harder - but easy to do during development 
(just add another, and see that you get a compile-time error).  Given 
that this is a sort of construct that you would rarely need in 
situations where you have so many initialisers that it is likely for you 
to make a mistake, it simply is not worth making an exception for it.

And if you really want to have compile-time checking of the number of 
initialisers, that is easy to do with a trick that works for any array 
initialisation (not just exported arrays):


#define WIDGETS_INIT { 1, 2, 3, 4 }
_Static_assert(sizeof((unsigned []) WIDGETS_INIT) == NUM_WIDGETS *
  			sizeof(unsigned), "Bad size");
unsigned widgets[] = WIDGETS_INIT;


> If a version of foo.h which is intended for 5 widgets gets used with a
> version of foo.c that only defines those four values, no diagnostic would
> be produced, but everything including foo.c would think there were five
> widgets numbered {1,2,3,4,0} rather than four.
> 

If you use a version of foo.h that is out of sync with foo.c like that, 
you have plenty of other problems to think about.

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