Re: tgmath.h
Ivan Perez <[email protected]> Tue, 26 Apr 2022 08:20:22 -0400
| Newsgroups | gmane.comp.hardware.avr.gcc |
|---|---|
| Message-ID | <CACZKWEJDRjrnbAP9gFTyxUFZT-TJ=_tqTRgoKDndtfo_j4cSWA@mail.gmail.com> |
--0000000000002772fe05dd8db892 Content-Type: text/plain; charset="UTF-8" Thanks!! This is very helpful! The hope is to generate the same C99 code regardless of the platform. So we need to use a math library that will work across platforms and deliver consistent behaviour. Ivan On Fri, Apr 22, 2022 at 5:22 AM David Brown <[email protected]> wrote: > On 22/04/2022 02:18, Ivan Perez wrote: > > Hi, > > > > I'm trying to compile a program for arduino that relies on tgmath.h to > > pick the right version of a function based on the types (the code is > > automatically generated by another tool). > > > > It's failing to compile with avr because tgmath.h is missing. > > > > I thought it was part of the standard C distribution since C99 and thus > > I'd be able to rely on it, but I'm no C expert. Does anyone know why it > > is not included in avr-libc? Any advice? > > > > I'm on Ubuntu 20.04, using: > > avr-libc 2.0.0+Atmel3.6.2-1.1 > > gcc-avr 5.4.0+Atmel3.6.2-1 > > binutils-avr 2.26.20160125+Atmel-3.6.2-2 > > > > Thanks, > > > > Ivan > > You are right that <tgmath.h> has been part of C since C99. But it is > not part of C++, which is what the Arduino tools use. <tgmath.h> has > macros for type-generic maths functions, and was originally implemented > with compiler-specific extensions. With C11, the "_Generic" feature can > be used to make compiler-independent implementations of the functions. > In C++, function overloading has existed from the beginning, and is done > in a completely different way. (Indeed, <tgmath.h> in C was invented to > give C programmers the convenience C++ users already enjoyed for their > maths functions, but made in a C-style manner.) > > So for your C++ code, you should use <cmath>, not <tgmath.h>, and > otherwise the usage will be the same. (You might need a "using > namespace std;", but the Arduino IDE likes to confusingly hide such > detail.) > --0000000000002772fe05dd8db892 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable <div dir=3D"auto">Thanks!! This is very helpful!</div><div dir=3D"auto"><br= ></div><div dir=3D"auto">The hope is to generate the same C99 code regardle= ss of the platform. So we need to use a math library that will work across = platforms and deliver consistent behaviour.</div><div dir=3D"auto"><br></di= v><div dir=3D"auto">Ivan</div><div dir=3D"auto"><br></div><div><div class= =3D"gmail_quote"><div dir=3D"ltr" class=3D"gmail_attr">On Fri, Apr 22, 2022= at 5:22 AM David Brown <<a href=3D"mailto:[email protected]">dav= [email protected]</a>> wrote:<br></div><blockquote class=3D"gmail_qu= ote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex= ">On 22/04/2022 02:18, Ivan Perez wrote:<br> > Hi,<br> > <br> > I'm trying to compile a program for arduino that relies on tgmath.= h to <br> > pick the right version of a function based on the types (the code is <= br> > automatically generated by another tool).<br> > <br> > It's failing to compile with avr because tgmath.h is missing.<br> > <br> > I thought it was part of the standard C distribution since C99 and thu= s <br> > I'd be able to rely on it, but I'm no C expert. Does anyone kn= ow why it <br> > is not included in avr-libc? Any advice?<br> > <br> > I'm on Ubuntu 20.04, using:<br> > avr-libc 2.0.0+Atmel3.6.2-1.1<br> > gcc-avr 5.4.0+Atmel3.6.2-1<br> > binutils-avr 2.26.20160125+Atmel-3.6.2-2<br> > <br> > Thanks,<br> > <br> > Ivan<br> <br> You are right that <tgmath.h> has been part of C since C99.=C2=A0 But= it is <br> not part of C++, which is what the Arduino tools use.=C2=A0 <tgmath.h>= ; has <br> macros for type-generic maths functions, and was originally implemented <br= > with compiler-specific extensions.=C2=A0 With C11, the "_Generic"= feature can <br> be used to make compiler-independent implementations of the functions. <br> In C++, function overloading has existed from the beginning, and is done <b= r> in a completely different way.=C2=A0 (Indeed, <tgmath.h> in C was inv= ented to <br> give C programmers the convenience C++ users already enjoyed for their <br> maths functions, but made in a C-style manner.)<br> <br> So for your C++ code, you should use <cmath>, not <tgmath.h>, a= nd <br> otherwise the usage will be the same.=C2=A0 (You might need a "using <= br> namespace std;", but the Arduino IDE likes to confusingly hide such de= tail.)<br> </blockquote></div></div> --0000000000002772fe05dd8db892--