On 2015-02-26, August Karlstrom <
fusio...@gmail.com> wrote:
> Is there a way to compile the C program below with the GCC options
> `-ansi -pedantic'? If not, is there any other way to get the absolute
> path of the running program and ensuring ANSI C conformance?
Note that GCC does not come with a complete C library, only very minimal
run-time support.
Roughly speaking, -ansi does two things. It disables certain non-conforming
language extensions in the compiler, and it defines the __STRICT_ANSI__
macro.
How third party library headers react to that macro is up to them.
The correct way to react to the macro is to hide all non-ANSI-C extensions,
and, secondly, also ensure that no language extensions are used in the header
itself that will break under -ansi.
SEcondly, the correct way to reveal extensions (like for instance the POSIX
fileno function in <stdio.h>) is to react to feature selection macros like
_POSIX_SOURCE (usually specified as -D_POSIX_SOURCE on the command line).
Unfortunately, environments do not implement this correctly.
* Cygwin: -ansi unconditionally hides all library extensions even if
you have feature selection macros like -D_POSIX_SOURCE.
* FreeBSD, possibly other BSDs: these people believe that feature
macros *restrict* rather than *reveal*. So for instance -D_POSIX_SOURCE means,
to the BSD people, not "please add 1990 POSIX to my order", but rather "I want
nothing but 1990 POSIX to be on my plate". So for instance -ansi and
-D_POSIX_SOURCE specifies a redundant intersection, equivalent to just -ansi,
because the fact that you specified -ansi means you want nothing but ANSI C:
no POSIX or anything else. In particular, if you want POSIX plus some
traditional BSD functions (ironically!) you can't do it in any documented way.
You do -D_POSIX_C_SOURCE=whatever and then add -D__BSD_VISIBLE, which is an
internal macro you're not supposed to be using.
In a sane environment like the GNU C Library, you just do -D_POSIX_SOURCE
and -D_BSD_SOURCE. Done! You have daemon in <unistd.h> and you have fileno
in <stdio.h>
* OS/X: Here, you just give up and use -D_DARWIN_C_SOURCE; `nuff said.
* MinGW: similar issues to Cygwin.
If you want to just switch the compiler to ANSI C mode, but not fight
the damned system header files, I recommend trying the following workaround,
with a caveat:
gcc -ansi -U__STRICT_ANSI__
The caveat is that this will screw up with header files which rely on
__STRICT_ANSI__ for conditionally selecting between ANSI constructs
and GCC extensions.
I use this as a workaround on broken platforms, not unconditionally.