Reputation: 21
#include <stdio.h>
#include <math.h>
int main()
{
int x, y, x1, x2, y1, y2;
float distance;
//take the 1st points coordinates x axis and y axis
printf("Enter the coordinates of 1st point: ");
scanf("%d %d", &x1, &y1);
//take the 2st points coordinates x axis and y axis
printf("Enter the coordinates of 2nd point: ");
scanf("%d %d", &x2, &y2);
x = x2 - x1;
y = y2 - y1;
distance = sqrt((x * x) + (y * y));
//display result
printf("Distance = %.2f", distance);
return 0;
}
when i compile the program an error message is shown in the terminal window.
/usr/bin/ld: /tmp/cc8GnBrR.o: in function 'main': distance2.c:(.text+0xa4): undefined reference to 'sqrt' collect2: error: ld returned 1 exit status
is there a permanent solution for this problem other than "gcc filename.c -o filename -lm"
Upvotes: 1
Views: 2359
Reputation: 1
Now, #include<Math.h> library does not come with Linux terminal call. This library only in C library so, it is unable to fetch the library using GCC compiler. So, we have to put flag(-)lm that is '-lm'.
Example:
your program is test.c
you have to compile in terminal:
=> gcc -o texecute test.c -lm
this '-lm' token helps you to compile the program successfully.
Upvotes: 0
Reputation: 25209
sqrt
is defined in libm
(the maths library). Unless you link with -lm
you will get an undefined symbol. Alternatives would include defining your own sqrt
function (don't do that).
Upvotes: 1