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


Groups > comp.lang.c > #93998

Re: To include or not to include

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.c
Subject Re: To include or not to include
Date 2016-09-24 23:55 +0100
Organization A noiseless patient Spider
Message-ID <87a8ewgb5y.fsf@bsb.me.uk> (permalink)
References <ns2tkb$p0$1@dont-email.me> <ns310l$bj5$1@dont-email.me> <ns3316$i3b$1@dont-email.me> <87zimyhk54.fsf@bsb.me.uk> <ns6kg9$eql$1@dont-email.me>

Show all headers | View raw


BartC <bc@freeuk.com> writes:

> On 23/09/2016 13:32, Ben Bacarisse wrote:
>> BartC <bc@freeuk.com> writes:
>>> On 23/09/2016 11:43, David Brown wrote:
>
>>>> 1. F.c should include F.h, to make sure that function definitions in F.c
>>>> and function declarations in F.h match.
>>>>
>>>> 2. I make sure that any functions or file-level variables in F.c are
>>>> either defined "static" in F.c, or they are declared "extern" in F.h and
>>>> have matching definitions in F.c.
>>>>
>>>> 3. Header F.h should include all the headers it needs in order to be
>>>> complete.  If G.c includes F.h, it should not have to include anything
>>>> else before or after it.  So if F.h declares a function that takes a
>>>> uint32_t, then F.h must include stdint.h.
>>>
>>> My rules would be a little different (using example module A):
>>>
>>> * A.h should only contain declarations for A.c that will be exported to
>>>   or shared with other modules
>>>
>>> * A.h should not contain any includes that are not needed for A.h
>>>   itself. Only includes that are necessary for A.h to compile as a
>>>   standalone file. (For example, including a header that contains
>>>   macros and typedefs used by A.h's declarations)
>>
>> In what way is this different?  Do you mean to imply that you *don't*
>> make sure that A.c includes A.h?  (That's the only thing I can see that
>> is different, but I assumed it was an omission rather than a rule.)
>
> I didn't see notice No. 2 rule. But I think my rules are stricter.
>
> Looking at the problems that headers need to solve, they aren't
> actually that straightforward. So given this set of names that are
> defined in module A (local or exported in A, or imported elsewhere):
>
> I_local         Local int
> J_shared        Exported/Imported int
> K_shared        Exported/Imported int (initialised)
>
> A_local         Local array
> B_shared        Exported/Imported array
> C_shared        Exported/Imported array (initialised)
>
> T_local         Local typedef
> U_shared        Exported/Imported typedef
>
> E_local         Local enum and enum names
> E_shared        Exported/Imported enum and enum names
>
> M_LOCAL         Local macro
> M_SHARED        Exported/Imported macro
>
> F_local         Local function
> G_shared        Exported/Imported function
>
> Then A's header A.h might be:
> -----------------------
> int J_shared;
> int K_shared;
>
> int B_shared[10];
> int C_shared[];
<snip>

> Shared variables only need declaring once, unless they are initialised
> then it's easier to declare them twice. (I don't use 'extern' on the
> shared declaration unless forced to by a compiler or linker).

That's an odd way of putting it.  Alternatively you could say that you
deliberately write undefined code but will correct it if the
implementation happens to say something about it.  Do you take the same
view of any other cases of undefined behaviour?

> One funny thing about a header such as A.h is that it declares
> *exported* names when compiled as part of A.c, but "imported" names
> must be declared when compiled as part of any other module. All using
> exactly the same syntax.

Another funny thing is that it provokes undefined behaviour (when used
as expected).  In effect, it's not really C.  Why would you write it
that way?

-- 
Ben.

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


Thread

To include or not to include James Harris <james.harris.1@gmail.com> - 2016-09-23 10:45 +0100
  Re: To include or not to include David Brown <david.brown@hesbynett.no> - 2016-09-23 12:43 +0200
    Re: To include or not to include BartC <bc@freeuk.com> - 2016-09-23 12:17 +0100
      Re: To include or not to include Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-09-23 13:32 +0100
        Re: To include or not to include David Brown <david.brown@hesbynett.no> - 2016-09-23 14:57 +0200
        Re: To include or not to include BartC <bc@freeuk.com> - 2016-09-24 20:34 +0100
          Re: To include or not to include Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-09-24 23:55 +0100
            Re: To include or not to include BartC <bc@freeuk.com> - 2016-09-25 01:43 +0100
              Re: To include or not to include Ian Collins <ian-news@hotmail.com> - 2016-09-25 14:38 +1300
              Re: To include or not to include Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-09-25 11:42 +0100
                Re: To include or not to include BartC <bc@freeuk.com> - 2016-09-25 11:58 +0100
                Re: To include or not to include James Harris <james.harris.1@gmail.com> - 2016-09-25 14:06 +0100
                Re: To include or not to include BartC <bc@freeuk.com> - 2016-09-25 15:15 +0100
                Re: To include or not to include Reinhardt Behm <rbehm@hushmail.com> - 2016-09-25 23:33 +0800
                Re: To include or not to include Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2016-09-25 10:04 -0600
                Re: To include or not to include Reinhardt Behm <rbehm@hushmail.com> - 2016-09-26 00:21 +0800
                Re: To include or not to include "James R. Kuyper" <jameskuyper@verizon.net> - 2016-09-25 13:17 -0400
                Re: To include or not to include James Harris <james.harris.1@gmail.com> - 2016-09-26 00:18 +0100
                Re: To include or not to include BartC <bc@freeuk.com> - 2016-09-26 17:55 +0100
                Re: To include or not to include Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-09-26 20:07 +0100
                Re: To include or not to include supercat@casperkitty.com - 2016-09-26 12:13 -0700
                Re: To include or not to include James Harris <james.harris.1@gmail.com> - 2016-09-27 01:25 +0100
                Re: To include or not to include supercat@casperkitty.com - 2016-09-26 18:05 -0700
                Re: To include or not to include Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-09-27 14:27 +0100
                Re: To include or not to include BartC <bc@freeuk.com> - 2016-09-27 14:49 +0100
                Re: To include or not to include Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-09-27 15:15 +0100
                Re: To include or not to include Malcolm McLean <malcolm.mclean5@btinternet.com> - 2016-09-27 07:42 -0700
                Re: To include or not to include Keith Thompson <kst-u@mib.org> - 2016-09-27 09:30 -0700
                Re: To include or not to include Malcolm McLean <malcolm.mclean5@btinternet.com> - 2016-09-27 10:06 -0700
                Re: To include or not to include supercat@casperkitty.com - 2016-09-27 08:12 -0700
                Re: To include or not to include Tim Rentsch <txr@alumni.caltech.edu> - 2016-09-28 10:46 -0700
                Re: To include or not to include BartC <bc@freeuk.com> - 2016-09-27 17:55 +0100
                Re: To include or not to include supercat@casperkitty.com - 2016-09-27 10:19 -0700
                Re: To include or not to include Keith Thompson <kst-u@mib.org> - 2016-09-27 11:42 -0700
                Re: To include or not to include BartC <bc@freeuk.com> - 2016-09-27 19:57 +0100
                Re: To include or not to include Keith Thompson <kst-u@mib.org> - 2016-09-27 12:38 -0700
                Re: To include or not to include BartC <bc@freeuk.com> - 2016-09-27 21:00 +0100
                Re: To include or not to include Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-09-28 01:50 +0100
                Re: To include or not to include BartC <bc@freeuk.com> - 2016-09-28 10:55 +0100
                Re: To include or not to include Malcolm McLean <malcolm.mclean5@btinternet.com> - 2016-09-27 13:33 -0700
                Re: To include or not to include Ike Naar <ike@iceland.freeshell.org> - 2016-09-28 19:13 +0000
                Re: To include or not to include supercat@casperkitty.com - 2016-09-27 12:38 -0700
                Re: To include or not to include James Harris <james.harris.1@gmail.com> - 2016-09-27 20:57 +0100
                Re: To include or not to include supercat@casperkitty.com - 2016-09-27 07:25 -0700
                Re: To include or not to include Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-09-28 00:28 +0100
                Re: To include or not to include supercat@casperkitty.com - 2016-09-30 07:53 -0700
                Re: To include or not to include Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-09-30 16:06 +0100
                Re: To include or not to include Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-09-27 13:53 +0100
                Re: To include or not to include James Harris <james.harris.1@gmail.com> - 2016-09-30 19:46 +0100
                Re: To include or not to include Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-09-30 20:27 +0100
                Re: To include or not to include Reinhardt Behm <rbehm@hushmail.com> - 2016-09-25 23:27 +0800
                Re: To include or not to include Jerry Stuckle <jstucklex@attglobal.net> - 2016-09-25 11:38 -0400
                Re: To include or not to include Reinhardt Behm <rbehm@hushmail.com> - 2016-09-25 23:40 +0800
                Re: To include or not to include BartC <bc@freeuk.com> - 2016-09-25 15:06 +0100
                Re: To include or not to include Jerry Stuckle <jstucklex@attglobal.net> - 2016-09-25 11:49 -0400
                Re: To include or not to include BartC <bc@freeuk.com> - 2016-09-25 18:05 +0100
                Re: To include or not to include Jerry Stuckle <jstucklex@attglobal.net> - 2016-09-25 16:42 -0400
                Re: To include or not to include BartC <bc@freeuk.com> - 2016-09-25 21:53 +0100
                Re: To include or not to include Jerry Stuckle <jstucklex@attglobal.net> - 2016-09-25 17:57 -0400
                Re: To include or not to include Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-09-25 21:20 +0100
                Re: To include or not to include BartC <bc@freeuk.com> - 2016-09-25 21:49 +0100
                Re: To include or not to include Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-09-26 00:40 +0100
      Re: To include or not to include David Brown <david.brown@hesbynett.no> - 2016-09-23 14:56 +0200
  Re: To include or not to include Richard Heathfield <rjh@cpax.org.uk> - 2016-09-23 13:31 +0100
    Re: To include or not to include James Harris <james.harris.1@gmail.com> - 2016-09-23 13:52 +0100
      Re: To include or not to include Richard Heathfield <rjh@cpax.org.uk> - 2016-09-23 14:35 +0100
    Re: To include or not to include Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-09-23 18:55 +0100
      Re: To include or not to include Richard Heathfield <rjh@cpax.org.uk> - 2016-09-23 20:33 +0100
  Re: To include or not to include Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2016-09-23 08:22 -0600
  Re: To include or not to include Richard Damon <Richard@Damon-Family.org> - 2016-09-23 11:37 -0400

csiph-web