energia / Energia

Fork of Arduino for the Texas Instruments LaunchPad's
http://energia.nu
Other
793 stars 673 forks source link

Stellaris LaunchPad / StellarPad: "undefined reference to `__errno'" for non-reentrant math functions #177

Closed horchler closed 11 years ago

horchler commented 11 years ago

My setup:

Hi. I am unable to compile code that calls non-reentrant math functions (i.e., #ifndef _REENT_ONLY in math.h), log() and sqrt() in particular. Doesn't seem to matter what type the arguments are, but I'm using doubles. Functions like sin() and cos() seem to work. The identical code compiles just fine in the Arduino IDE. I've tried explicitly adding includes for math.h and errno.h and even machine/fastmath.h to no avail. I've tried explicitly #undef-ing _REENT_ONLY. This is an example error:

/Applications/Energia.app/Contents/Resources/Java/hardware/tools/lm4f/bin/../lib/gcc/arm-none-eabi/
   4.7.1/../../../../arm-none-eabi/lib/thumb/armv7e-m/fpu/fpv4-sp-d16/libm.a(lib_a-w_sqrt.o): 
   In function `sqrt':
/Users/rwessels/yagarto/newlib-build/arm-none-eabi/thumb/armv7e-m/fpu/fpv4-sp-d16/newlib/libm/math/
   ../../../../../../../../../newlib-1.20.0/newlib/libm/math/w_sqrt.c:81: 
   undefined reference to `__errno'
collect2: error: ld returned 1 exit status

The path to w_sqrt.c looks like it's on someone else's machine, similar to this issue: https://github.com/energia/Energia/issues/122

I have also seen this issue, but my problem seems to be different as calling logf() or sqrtf() results in the same error: https://github.com/energia/Energia/issues/110

Thank you for any suggestions. -- Andy

robertinant commented 11 years ago

Thanks for the report. Obviously we didn't test for this.

This is a linker order issue. __errno is defined in libc but Energia places -lm after -lc and hence the linker error. Will fix it for the next release.

You can work around it by defining placing int __errno in your main Sketch. This is is OK as long as your program does not depend on errno being set in the math functions you use from libm; they set errno for out of range parameters like log (0) and log (-1).

Robert

horchler commented 11 years ago

Robert,

Thank you. I confirm that the workaround works as advertised. I had implemented my own fast exp(), log(), and sqrt() functions in the interim, but I really needed the real double precision math functions for the simulation of differential equations (one of my reasons for using the Stellaris LM4F in the first place).

I look forward to the next release. One thing. I've noticed that Energia seems to be the only place to use the term "StellarPad". This made searching for help on this issue (and other stuff) needlessly complicated. Also, when first checking out the board, it took me a bit to realize that Energia supported the Stellaris LaunchPad and that it was the same thing as the "StellarPad". Would it be possible to change mentions of this hardware in your documentation (and in the software) to "Stellaris" or "Stellaris LaunchPad"? After all, that is the name of the product, as far as I'm aware.

-- Andy

robertinant commented 11 years ago

Happy to hear that you got it working. In have already pushed the fix up to the repo. Good to know that it is hard to figure out that Energia supports the Stellaris Launchpad. I will get some meta data referring to it as Stellaris LaunchPad.