1

I am facing error in compiling this code

#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) );

}

The error is:

/tmp/ccsWKzC6.o: In function `main':
p2.c:(.text+0x9b): undefined reference to `sqrt'
collect2: error: ld returned 1 exit status

I know it can be fixed by using -lm in gcc, but that's not the trouble, the trouble is the following code compiles and run with no errors:

#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));

}

Why is it happening that I need to link math library sometimes only?

frunkad
  • 2,433
  • 1
  • 23
  • 35

0 Answers0