Discuss here different C compiler set ups, and compiling executables for the ESP8266

User avatar
By smh
#48980 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
User avatar
By jcmvbkbc
#49001
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.
User avatar
By smh
#49027 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
User avatar
By jcmvbkbc
#49046
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.