我在编译此代码时遇到错误
#include <stdio.h>
#include <math.h>
#define pi 3
void main()
{
double a = 10,b=11,c=13,s,t,p;
s = a+b+c;
s/=2.0;
p= s*(s-a)*(s-b)*(s-c);
printf("%f\n",sqrt(p) );
}
错误是:
/tmp/ccsWKzC6.o: In function `main':
p2.c:(.text+0x9b): undefined reference to `sqrt'
collect2: error: ld returned 1 exit status
我知道可以通过在gcc中使用-lm
来修复它,但这不是麻烦,问题是以下代码编译并运行时没有错误:
#include <stdio.h>
#include <math.h>
#define pi 3
void main()
{
double a = 10,b=11,c=13,s,t;
printf("%f",sqrt(10.2));
}
为什么我有时只需要链接数学库?