一个gcc sqrt函数bug?

Yin*_*Zhu 5 c gcc

以下程序无法在gcc中编译.但它与g ++和MSC++的.c扩展编译正常.

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

int main()
{
  double t = 10;
  double t2 = 200;

  printf("%lf\n", sqrt(t*t2));

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

我的系统是CentOS,版本信息.

> gcc --version
gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-46)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Run Code Online (Sandbox Code Playgroud)

错误信息:

> gcc test.c
/tmp/ccyY3Hiw.o: In function `main':
test.c:(.text+0x55): undefined reference to `sqrt'
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)

这是一个错误吗?

任何人都可以为我做测试吗?

Tom*_*Tom 19

你有没有连接数学库?

gcc -lm test.c -o test 
Run Code Online (Sandbox Code Playgroud)

  • 因为`g ++`引入了`-lddc ++`,它引入了`-lm`. (6认同)
  • 你不想用`g ++`编译'原始'C代码.C和C++是不同的语言. (2认同)
  • 我使用的gcc在*test.c之后需要`-lm`*(在Ubuntu上为4.6.1) (2认同)