c - Strange Numerical Behaviour of gcc Linker -


i have problem simple c code following:

#include <math.h> #include <stdio.h>  int main () {     printf ("%f\n", exp(1)); } 

replacing 1 numbers smaller 710 results in successful compilation expected effect, numbers higher , linker error, of things:

/tmp/ccqvnsno.o: in function `main':  test.c:(.text+0x1c): undefined reference `exp' collect2: error: ld returned 1 exit status 

i have tested numbers under 1000 following bash script:

for in {0..1000};     sed -i -r "s:[0-9]+:${i}:" test.c     gcc -o test test.c     ./test done 

putting printf statements in loop exp of index variable results in same linkage error, regardless of upper bound.

what's going on here? compiler recognizing 710 sort of limit long double? why linker catching error? sorry credulity, i'm new c.

my guess smaller numbers gcc compiler optimizes exp call else, larger numbers needs standard implementation in math library. , math library needs explicitly linked with.

you link math library adding option -lm when linking. it's option -l (lower-case l) tells linker link library, , m math library.


regarding possible optimizations of the exp function can call small number, , use disassembler check generated code.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -