为什么在尝试在C中使用math.h中的日志时会出现此错误?

时间:2013-08-03 02:04:53

标签: c logarithm math.h

我需要在程序中使用日志进行分配。我在我的机器上运行了这个测试程序,看看日志功能是如何工作的(如果可以的话),我在构建过程中遇到了以下错误。

代码

/* log example */
#include <stdio.h>      /* printf */
#include <math.h>       /* log */

int main()
{
  double param, result;
  param = 5.5;
  result = log (param);
  printf ("log(%f) = %f\n", param, result );
  return 0;
}

错误

gcc -Wall -o "test" "test.c" (in directory: /home/linux/Development/codetest)
/tmp/ccCDqX7x.o: In function `main':
test.c:(.text+0x1b): undefined reference to `log'
collect2: ld returned 1 exit status
Compilation failed.

链接

这是从这个tutorial网站抓取的C99代码。

1 个答案:

答案 0 :(得分:4)

-lm添加到编译命令以链接数学库。

gcc -Wall -o "test" "test.c" -lm