I've been meaning to follow up on why libm is not required (and therefore why the placement of the -lm doesn't matter) when you change the line to a constant expression that can be evaluated at compile time
You can kind of see how this happens if you use the -S option to stop compilation at the assembler stage, and then look for what function calls are present the assembly file. So for example if we have the original non-constant version:
Code:
$ cat sin1.c
#include <stdio.h>
#include <math.h>
int main()
{
double p = 3.1415926;
double v;
v = sin(p/4.0);
printf("%lf\n", v);
return 0;
}
then
Code:
$ gcc -S sin1.c
$
$ grep 'call' sin1.s
call sin
call printf
$
but if we replace the sin argument by a constant expression:
Code:
$ cat sin2.c
#include <stdio.h>
#include <math.h>
int main()
{
double p = 3.1415926;
double v;
v = sin(3.1415926/4.0);
printf("%lf\n", v);
return 0;
}
then
Code:
$ gcc -S sin2.c
$
$ grep 'call' sin2.s
call printf
$
Hope this helps
Bookmarks