[ GCC ] 编译和链接的问题

有如下程序:

#include <stdio.h>
#include <math.h>

int main() {
  double a, b, c, s, area;
  a = 3.67;
  b = 5.43;
  c = 6.21;
  s = (a + b + c) / 2;
  area = sqrt(s * (s - a) * (s - b) * (s - c));
  printf("a = %lf\t b = %lf\t c = %lf\n", a, b, c);
  printf("area = %lf\n", area);
  return 0;
}

执行如下命令进行编译:

gcc -o test test.c

提示如下错误:

test.c:(.text+0x826): undefined reference to `sqrt'

解决方案:

在执行编译语句的时候,加入'-lm',链接数学库,以上问题可以解决。