-->
Page 1 of 3

Question about math.h

PostPosted: Sat Jun 11, 2016 5:38 am
by smh
I have a question about using math.h with the open sdk

This compiles:
Code: Select allfloat foo = sin(3.14159);


But this does not:
Code: Select allfloat mypi = 3.14159;
float foo = sin(mypi);


The error is confusing me because it's an undefined reference to the function sin.

Code: Select alluser_main.c:(.irom0.text+0xb4): undefined reference to `sin'
user_main.o: In function `my_function':
user_main.c:(.irom0.text+0x115): undefined reference to `sin'
collect2: error: ld returned 1 exit status


Anyone got some clues on this?

Thanks,
smh

Re: Question about math.h

PostPosted: Sat Jun 11, 2016 11:37 am
by jcmvbkbc
smh wrote:This compiles:
Code: Select allfloat foo = sin(3.14159);


sin is a pure function, the compiler calculates the result at compile time. There's no function call in the generated code, only load of the constant to the variable foo.

smh wrote:But this does not:
Code: Select allfloat mypi = 3.14159;
float foo = sin(mypi);


The error is confusing me because it's an undefined reference to the function sin.

Code: Select alluser_main.c:(.irom0.text+0xb4): undefined reference to `sin'
user_main.o: In function `my_function':
user_main.c:(.irom0.text+0x115): undefined reference to `sin'
collect2: error: ld returned 1 exit status


This actually compiles as well, because you get linking error, not compilation error. But this time the compiler is not smart enough (or not enough optimizations are enabled) to see that the result is a constant. So there's a call to function sin in the generated code. You need to add -lm to the compiler command line to link with math library, where this function is defined.

Re: Question about math.h

PostPosted: Sat Jun 11, 2016 6:13 pm
by smh
H, thanks for the response. I am linking with -lm. Extract from my Makefile below. Hence my confusion.

Code: Select allLDFLAGS= -nostdlib \
-L $(ESP8266_SDK_ROOT)/lib -L $(ESP8266_SDK_ROOT)/xtensa-lx106-elf/xtensa-lx106-elf/include \
-L $(ESP8266_SDK_ROOT)/ld \
-T $(ESP8266_SDK_ROOT)//xtensa-lx106-elf/xtensa-lx106-elf/sysroot/usr/lib/eagle.app.v6.ld\
        -Wl,--no-check-sections -u call_user_start -Wl,-static -Wl,--start-group \
        -lm -lc -lgcc -lhal -lphy -lpp -lnet80211 -llwip -lwpa -lmain -ljson -lupgrade -lssl \
        -lpwm -lsmartconfig -Wl,--end-group

Re: Question about math.h

PostPosted: Sun Jun 12, 2016 2:20 am
by jcmvbkbc
smh wrote:H, thanks for the response. I am linking with -lm. Extract from my Makefile below. Hence my confusion.

Then you probably don't have sin in the math library archive.
I'm testing it with the latest open-esp-sdk, and sin is there, but with default settings the resulting code doesn't fit into iram.