Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #103858
| From | David Brown <david.brown@hesbynett.no> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Is 'extern' necessary? |
| Date | 2017-02-14 09:28 +0100 |
| Organization | A noiseless patient Spider |
| Message-ID | <o7uf0l$s1d$1@dont-email.me> (permalink) |
| References | (4 earlier) <o7sd8q$enk$1@dont-email.me> <ln7f4ut1va.fsf@kst-u.example.com> <o7t0ns$pnt$1@dont-email.me> <o7tfl8$lta$1@dont-email.me> <o7tkra$4al$1@dont-email.me> |
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. Was your only
purpose in looking it up and posting it so that you can mock something
you don't understand?
> 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), it does not name the parameters, it provides no extra
information to the compiler for optimisation usage (maybe lccwin simply
does not have the same kind of optimisations), and it does not provide
information to allow the kind of extremely useful format checking
features of gcc.
As for my own headers, you are apparently totally misunderstanding the
difference between basic libraries and user code. Let me explain.
User code is written by the user. It must follow the
implementation-defined rules of C (i.e., the language defined by the
standards along with any implementation-specific features). It should
be written to be readable and maintainable, with the user's choice of
balance between development time, clarity, run-time efficiency, etc. It
can be written to work with lots of compilers or just one, many compiler
options or just one set, many C standards or just one.
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. It does not have to follow the rules - it can
"cheat", as long as the compiler is in on the game - the library writer
can put things in the headers and implementation files that are not
allowed in user code. It is written by a few people, but used by
millions - the emphasis is very solidly on the side of run-time
efficiency rather than developer efficiency. It must work regardless of
the compiler options, and usually regardless of the compiler version -
sometimes regardless of the compiler vendor. It typically must work
regardless of the language, supporting C and C++ and possibly others
such as Objective-C. And it must match the standards exactly.
So /I/ would be very unlikely to have as much complications in a
declaration as this one does. (If I were writing a printf-style
function, I /would/ use the "format" attribute.) Most of my
declarations are relatively simple in practice, though I would never
write the lazy way of lccwin's declaration without parameter names.
However, I would expect to see a different level entirely in a standard
library header - the example you gave looks like it is from someone who
does not care about what he is doing.
(Note - because the compiler and the library work together to form the
implementation, it is quite possible for them to have extra hidden
features that are not apparent in the declarations in the headers. The
compiler can quite reasonably assume that the "restrict" qualifiers are
in the right places, or know about checking format strings for the
printf family even though it is not expressed in the header. I am only
commenting on the single declaration you quoted - lccwin may be more
advanced than that declaration suggests.)
>
>> or scared that you won't understand what you read, or merely worried
>> that if you actually learn what this means then you will lose some
>> opportunities to moan about gcc.
>>
>> First, you have to realise that this is a library, it is not part of
>> gcc. It is written in a way that will work with many compilers (hence
>> some macros are used),
>
> I tried that header with 4 other compilers (compiling hello world with
> the include search path set to the one used by this gcc), and they all
> failed.
If you look at the name of the function, it is "__mingw_vsnprintf". You
will probably find that "vsnprintf" is declared elsewhere, or perhaps
there are weak symbols, aliases, or other compiler-library interactions.
I don't know whether this particular library will work on other
compilers, but certainly some libraries are portable between compilers.
The choice of compilers will, of course, be limited - a standard
library is part of the implementation. It may work with clang, for example.
>
> but that it should be as optimal as possible
>> across a wide range of gcc versions (and compiler flags) since it is
>> most commonly used with gcc. It also aims to give as good static error
>> checking as possible.
>> This it uses "__restrict__", which will work like "restrict" on C99 for
>> more efficient code generation - but will also work on gcc with other
>> choices of standards and in C++. It has the "format" attribute, to give
>> error checking - gcc can warn you if your printf format does not match
>> with the actual parameters (for a fixed format string).
>
> lccwin also does that checking; /its/ vsnprintf looks like this:
>
> int vsnprintf(char *,size_t,const char *,va_list);
>
> (How on earth does it manage that?)
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.
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? As I say, it is perfectly
possible for the compiler to manage this without it being in the header.
But gcc chose to implement this using function attributes. This means
it works fine with various language standards, or with libraries with
extra features. It also works with user code - I can define a
"debugPrintf" or "uartPrintf" function and get the same kind of checking.
>
> You seem to constantly think that things that are bigger and more
> complicated are always better and always justified.
>
>>>
>>> This is what that function declares:
>>>
>>> extern vsnprintf(char*, size_t, char*, va_list);
>>>
>>> I think, beyond being in text format, they've given up on making this
>>> stuff human-readable. Or tidy and compact.
>>
>> Yes, but the version from the library you gave does more. It is also
>> more informative (the parameters have names),
>
> This is the same function in the MSDN docs, and the params have names:
>
> int vsnprintf(
> char *buffer,
> size_t count,
> const char *format,
> va_list argptr
> );
Parameter names are an improvement, but the declaration is still wrong
for C99 onwards. And again, it does not have the extra static error
checking features (again, these may be built into the compiler).
>
>>and it is correct (it has a return type).
>
> Yes, the int. Maybe I was using 'implicit' int, or maybe I missed a
> 3-letter return type lurking somewhere in the /middle/ of all that
> line-noise.
The "line-noise" in question was the declaration from lccwin... But
yes, I realise that was probably a typo.
However, the declaration from the standards (C99, C11) is:
int vsnprintf(char * restrict s, size_t n,
const char * restrict format,
va_list arg);
Note the "restrict" part. It is a /long/ time since C99 was standard.
The gcc use of "__restrict__", which you see as ugly, complex, and
apparently difficult to understand, lets that declaration work in C90
mode and in C++ (where gcc supports it as an extension).
>
> If implementers don't want this to be readable, then why bother having
> it as source code? That must be reprocessed over and over again. There
> must be more efficient ways of doing it, and AFAIK C doesn't stop an
> implementation from having standard headers inside the compiler already
> built-in.
>
It is perfectly readable, to anyone who wants to make a small effort to
/read/ it rather than to complain about it. The compiler is quite happy
with it.
You are right that a compiler can have some or all of the information
about the library headers and implementation built-in. Some do. You
are also right that this could make an implementation a good deal more
efficient. gcc does not do this - it has /knowledge/ of the compiler
library built in, and can turn some printf() calls into puts(), and
inlines memcpy() and optimises str* functions. But most of the library
and headers are external for maximum flexibility - people use many
different libraries with gcc.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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