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


Groups > comp.lang.c > #103873

Re: Is 'extern' necessary?

From David Brown <david.brown@hesbynett.no>
Newsgroups comp.lang.c
Subject Re: Is 'extern' necessary?
Date 2017-02-14 13:29 +0100
Organization A noiseless patient Spider
Message-ID <o7ut4a$9gu$1@dont-email.me> (permalink)
References (6 earlier) <o7t0ns$pnt$1@dont-email.me> <o7tfl8$lta$1@dont-email.me> <o7tkra$4al$1@dont-email.me> <o7uf0l$s1d$1@dont-email.me> <o7uqci$bg$1@dont-email.me>

Show all headers | View raw


On 14/02/17 12:42, BartC wrote:
> On 14/02/2017 08:28, David Brown wrote:
>> On 14/02/17 02:02, BartC wrote:
>>> On 13/02/2017 23:33, David Brown wrote:
>>>> On 13/02/17 20:19, BartC wrote:
>>>
>>>>> It's a mystery why gcc-related headers seem to be so full of junk
>>>>> compared with those of other compilers.
>>>>
>>>> Basically, what you are saying is "I don't understand this.  Therefore
>>>> it must be junk".  I don't know whether you are too lazy to look it up,
>>>
>>> Why would I want to look it up?
>>
>> You must have had some interest in the details to look up the function's
>> declaration in the header file - otherwise you would do what everyone
>> else does, and use a more practical reference for the vsnprintf
>> function, such as:
>>
>> <http://en.cppreference.com/w/c/io/vfprintf>
>>
>> So you are interested in how it is declared, yet now you are saying that
>> you are not interested in what these details mean.
> 
> You should have read from further up the thread. I was investigating the
> use of 'extern' in various headers, and came across this.

Fair enough.

> 
> It's like looking to see what kind of plugs are used in the sockets for
> various appliances, and coming across various 'christmas tree'
> arrangements with a dozen plugs plugged into one socket via multiple
> adaptors. You're not interested in what those extra plugs are for, only
> that it looks wrong.

The difference here is that you know enough about plugs to know that
this is a bad idea.  But with the variety of attributes and qualifiers
in this header, /you/ don't know enough about them to judge if they are
appropriate or not.  All you can say is that it offends your aesthetic
sense of code that looks nice - which is, of course, irrelevant in such
internal code that is meant to be read by the compiler, not the programmer.

> 
>>> Look at the lccwin or MSDN versions
>>> below; /that's/ how you want these things expressed. Short and sweet. (I
>>> don't want to think about what your own headers look like if you think
>>> this is reasonable.)
>>
>> I don't have lccwin or MSDN, and have no interest in them - so no, I
>> won't look that up.  You posted lccwin's version and it is far inferior
>> for a library call.  It does not follow the standards (at least, not C99
>> onwards),
> 
> Actually it's closer to what is given in the C standard than MingW's
> version.

It does not have "restrict".  There is nothing to stop an implementation
/adding/ information - but it should not skip things.

> 
>  it does not name the parameters,
> 
> What's the purpose of that? If you say human documentation, then you're
> having a laugh. For some reason or other, they're putting half of the
> gubbins needed to implement it into the public interface.

Let us then say it is for inhuman documentation - for use by those weird
creatures that are able to read reference manuals, use google, think,
and in other ways understand the way the declaration is written.  It is
also for IDEs that can show the parameter names as hints while typing,
or documentation generators like doxygen that can digest the header to
generate more human-friendly information.

> 
>  it provides no extra
>> information to the compiler for optimisation usage (maybe lccwin simply
>> does not have the same kind of optimisations),
> 
> This is at odds with what you've always said in the past about keeping
> explicit optimisation hints out of the source code, and letting the
> compiler just do its job.

No, you must have misunderstood me.  My principle is to give the
compiler as much information as possible, then let it generate the best
code it can.  I advise against trying to do "hand optimisation" (or
inventing constructs for hand optimisation when designing a new
language).  But if you can give the compiler more information, then it
can do a better job of optimising and also static error checking.

> 
>  and it does not provide
>> information to allow the kind of extremely useful format checking
>> features of gcc.
> 
> I've already said that lccwin does exactly the same kind of
> format-checking.

I don't have experience with lccwin, so I can't comment (except as I
have done earlier about how this could be implemented).

> 
>> Standard library code is part of the implementation.  Even if it is
>> developed separately from the compiler, it is still in close cooperation
>> with the compiler.
> 
> "The" compiler? I thought such libraries were completely independent
> from any one compiler and were all interchangable?
> 
> Or you finally admitting that perhaps the MingW library /is/ really
> designed to work with gcc?

Standard libraries are never going to be /completely/ independent or
interchangeable - but they certainly can be somewhat independent.  It
might be that the library you have here only works with the particular
build of gcc that you have, but it is certainly common that a library
used for gcc will work on a range of gcc targets and sizes, different
gcc versions, different C standards, and likely also at least clang.  It
could perhaps also work with Intel's icc, maybe Pathscale, and other
*nix compilers.  You can read up about various standard library
implementations and how portable they are.

So I should have written "compilers", rather than "the compiler".

> 
> 
>> The "vsnprintf" function is defined in the standard - the compiler knows
>> what it should do, and can check the format string.  Whether it does
>> that or not, I don't know.
> 
> So what's the point of the 'format' stuff then in the MingW code?

That is gcc's (and clang's, and maybe other compilers') method of
telling the compiler that this is a printf-style function, and which
parameters are to be used for format checking.  This means that gcc can
support a single attribute and method, and use it for any such function.
 It is an alternative to hard-coding the functionality in the compiler.

> 
>> int main(void) {
>>   int a = 1;
>>   const char* s = "Hello";
>>   printf("Text: %s Data: %i\n", a, s);
>> }
>>
>> With -Wall, gcc will warn that "%s" expects an argument of type "char*",
>> but argument 2 has type "int".
>>
>> Does lccwin do that kind of checking?
> 
> lccwin gives me this with default options:
> 
> Warning c.c: 5  printf argument mismatch for format s. Expected char
> pointer got int
> Warning c.c: 5  printf argument mismatch for format i. Expected int got
> pointer to char
> 

Marvellous.  I am a big fan of doing as much static error checking as
practically possible.

> gcc says nothing with default options. gcc -pedantic-errors says
> nothing. I have to use one of the -Wxxx options to get it to report it.
> So not only do I need the macguffin in the header file, I need a
> counterpart in the compiler options.

Well, yes.  The code is allowed to have such mismatches.  gcc is an
extremely flexible tool, and people use it in a wide variety of ways.
Some people want lots of warnings, some people want none.  Some people
want optimisations, others want none.  And so on.  lccwin is a more
niche tool, and is very much the product of one person - the way it
works and the default functionality is picked according to what Jacob
thinks is good.  Now, I happen to agree with him that enabling warnings
by default is a good thing.  But there are a gazilion gcc users out
there, and each will have a different opinion on which combination of
gcc warnings is best.  So you specify it on the command line.


> 
> (Actually I tried to comment out the format attribute stuff from stdio.h
> to see if gcc still managed it. But it's so complicated I can hardly
> find the printf declaration. I completely commented out what looks like
> the one, and it still worked. It didn't even give an 'implicit
> declaration of printf' warning. So it's seeing it somewhere, but where?
> Hiding the stdio.h file did give an error about not finding stdio.h, so
> it was the right file; it wasn't sneakily using a version elsewhere.

It turns out that gcc will check the format identifiers for printf even
without including <stdio.h> at all.  Whether it will do that with
vsnprintf or not, I haven't tested - it might well do so.  But perhaps
the library authors are aware of issues that we are not.  Maybe earlier
versions of gcc (for which this library is still valid) need it, perhaps
other compilers need it, perhaps it is needed when using different
compiler flags, or perhaps the authors simply felt it was the correct
thing to do - put the format attribute in explicitly.

> 
> THIS is what it's all about; when things get sticky enough that you have
> to start investigating things, then gcc or anything associated with it
> makes it much harder than with any other compiler (msvc excepted).

You have demonstrated clearly that /you/ think it is hard, but nothing
you have written makes it appear hard to me.  And nothing indicates that
any of this is relevant to your average C programmer who simply uses
these functions without bothering about the details.

> 
> But perhaps that's to be expected with a compiler that chooses path
> names so difficult to type that you usually have to copy&paste:
> 
>   \tdm\x86_64-w64-mingw32\include
> 
> and with a back-up include path for the standard headers it can't find
> there:
> 
>  \tdm\lib\gcc\x86_64-w64-mingw32\5.1.0\include)
> 

You are complaining about path names?  The name (which is chosen by the
people building your mingw copy, it is not fixed by gcc) conveniently
shows target and version numbers.  As someone who has many compilers for
many targets and with many versions, I am very glad to see such names.
If I don't want to type them, copy and paste works as does tab
completion, or selection from a file browser.  Certainly such names are
vastly easier to work with than some monstrosity under "Program Files
(x86)".

>   As I say, it is perfectly
>> possible for the compiler to manage this without it being in the header.
> 
> You make it sound like a dog walking on its hind legs. Of course it can
> do it if a compiler 'knows' about *printf functions.
> 
> 

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


Thread

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

csiph-web