Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

a gcc sqrt function bug?

Tags:

c

gcc

The following program cannot compile in gcc. But it compiles OK with g++ and MSC++ with .c extension.

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

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

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

  return 0;
}

My system is CentOS, the version info.

> 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.

The error info:

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

Is this a bug?

Any one can do a test for me?

like image 835
Yin Zhu Avatar asked Dec 30 '09 17:12

Yin Zhu


3 Answers

Have you linked the math library?

gcc -lm test.c -o test 
like image 198
Tom Avatar answered Sep 30 '22 12:09

Tom


Add the math library with flag -lm

> gcc test.c -lm
like image 37
Drew Dormann Avatar answered Sep 30 '22 11:09

Drew Dormann


Try gcc -lm test.c -o test

For gcc, you need to tell it to link the math library in, by adding -lm to your gcc call.

like image 25
Prasoon Saurav Avatar answered Sep 30 '22 12:09

Prasoon Saurav