Feature Conditional for M_PI

Corinna Vinschen vinschen@redhat.com
Mon Aug 31 15:27:15 GMT 2020


On Aug 31 10:10, Joel Sherrill wrote:
> Hi
> 
> I was porting some code from Linux to Cygwin and came across this. M_PI in
> math.h is defined by POSIX as part of XSI. It does not appear to be part of
> C99 or C++03.  I have this cut down to show the problem:
> 
> ==========================
> #include <math.h>
> 
> double pi = M_PI;
> ==========================
> 
> And this script to try various feature defines and compilers:
> 
> ===========================
> GCC=${GCC:-g++}
> 
> ${GCC} -c m.c
> ${GCC} -D_XOPEN_SOURCE=700 -c m.c
> ${GCC} -D_POSIX_C_SOURCE=200809L -c m.c
> ${GCC} -D_XOPEN_SOURCE=700 -c -D_POSIX_C_SOURCE=200809L -c m.c
> ===========================
> 
> All of those compiler invocations work on Linux but the third one does not
> work on Cygwin or RTEMS which use newlib.
> 
> Is the proper thing to do to add  -D_XOPEN_SOURCE=700 when compiling this
> program?
> 
> Just curious if Linux is defining _XOPEN_SOURCE by default and newlib
> doesn't.

In glibc's math.h, M_PI is guarded with

  #if defined __USE_MISC || defined __USE_XOPEN

In newlib, it's guarded with

  #if __BSD_VISIBLE || __XSI_VISIBLE

Note that this is identical to the guards on at least FreeBSD.

In both cases, newlib as well as glibc, "MISC" is defined by default,
but "BSD" isn't.  That's why your 3rd invocation fails on BSDs and
newlib/Cygwin, but not on Linux.

> Not sure what's the right answer. So asking.

Corinna



More information about the Newlib mailing list