Feature Conditional for M_PI

Joel Sherrill joel.sherrill@gmail.com
Mon Aug 31 21:19:35 GMT 2020


On Mon, Aug 31, 2020 at 3:05 PM Corinna Vinschen via Newlib <
newlib@sourceware.org> wrote:

> On Aug 31 10:50, Joel Sherrill wrote:
> > On Mon, Aug 31, 2020 at 10:30 AM Corinna Vinschen via Newlib <
> > newlib@sourceware.org> wrote:
> >
> > > On Aug 31 17:27, Corinna Vinschen via Newlib wrote:
> > > > 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.
>
> Actually, Yaakov pointed out on IRC that this is a C++ problem on Linux.
> You're running the above test with g++.  If you perform the third test
> with gcc on Linux:
>
>   gcc -D_POSIX_C_SOURCE=200809L -c m.c
>
> you'll get the same error as on newlib and FreeBSD:
>
> m.c: In function ‘main’:
> m.c:8:15: error: ‘M_PI’ undeclared (first use in this function)
>     8 |   double pi = M_PI;
>           |               ^~~~
>           m.c:8:15: note: each undeclared identifier is reported only once
> for each function it appears in
>
> The problem in glibc and/or g++ is that this does *not* occur when
> building with g++.
>

Thanks. I was building with g++ because the code this came up in was C++.
In the application, I just defined the proper thing to make it visible.

Was this the right thing to do? Or is there something else wrong? Honestly,
the odd C library things that happen from C++ is often hard to explain.

--joel



>
>
> Corinna
>
>


More information about the Newlib mailing list