- Sat Jun 11, 2016 11:37 am
#49001
smh wrote:This compiles:
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.