On 05/04/2011 13:09, richardcavell@xxxxxxxx wrote:
I'm writing an open source project that is designed to run on all kinds
of obscure architectures. My project relies on libcurl, which is in C89
and very portable. I don't want to limit the users' choice of compiler
unnecessarily. So, for example, I use malloc instead of variable length
arrays. Having said that, using long string literals seems pretty
harmless and I can't figure out a way to avoid usage of the other three
C99 features while retaining their functionality.
If you are using C99 features you /are/ limiting the user's choice of
compiler. But most compilers support at least the most common C99
features - even compilers on obscure architectures (I work with embedded
programming, and have plenty of experience with obscure architectures
and limited compilers!).
It can make sense to limit certain C99 features - you mention avoiding
variable length arrays, which is a good example of a C99 feature that
many compilers do not implement properly.
But somewhere, sometime, you have to draw a line - you can't support
everybody and every tool. If your code is better because it uses some
C99 features, then use those features and write code that is better for
98% of your potential users. Your code then conforms to C99 standards,
but /may/ work on some non-C99 compilers.
mvh.,
David
Richard
-----Original Message-----
From: David Brown <david@xxxxxxxxxxxxxxx>
To: gcc-help@xxxxxxxxxxx
Sent: Tue, Apr 5, 2011 8:47 pm
Subject: Re: How to use C89 with certain C99 features
On 05/04/2011 12:32, richardcavell@xxxxxxxx wrote:
Hi, everyone. I'm building a project in C. My idea is that I want it
to
be mostly C89, except that I will be using certain features of C99.
What
compiler flags do I need to tell GCC what I want?
What are you actually trying to achieve? The only reason I can think of
for someone to write C89 standard is to be compatible with more limited
or older compilers. If that's the case, then you should write C89 - not
C99. Many compilers that are basically C89 (or C90, or ANSI) will
accept a few C99 features, such as // comments. But they are very
unlikely to accept more complex C99 features, such as newer ways to
handle variadic parameters. Thus your code will only work with a proper
C99 compiler, and thus might as well use --std=c99.
At present, if I compile with -std=c99, I get no errors or warnings at
all. If I compile with -std=c89, I get the following warnings:
(When I use macros with ... and __VA_ARGS__)
warning: anonymous variadic macros were introduced in C99
(When I use long string literals)
warning: string length '613' is greater than the length '509' ISO C90
compilers are required to support
(When I use vsnprintf)
warning: implicit declaration of function 'vsnprintf'
(When I initialize a struct with run-time data)
warning: initializer element is not computable at load time
Richard