lets consider simple program below
this program will compile but linker fails to find definition of cos and sin functions
#include<stdio.h>
#include<math.h>
#include <stdlib.h>
#define PI 3.142
int main()
{
double sinval, cosval;
int x=2;
sinval = sin(PI/x);
cosval = cos(PI/x);
printf("sin and cos of PI/x is %lf %lf\n", sinval, cosval);
return 0;
}
followings link errors we will hit, since linker can't able to resolve the definition of cos() and sin()
shiv@ubuntu:~$ gcc mat1.c
/tmp/ccHOvWsZ.o: In function `main':
mat1.c:(.text+0x2a): undefined reference to `sin'
mat1.c:(.text+0x4b): undefined reference to `cos'
collect2: ld returned 1 exit status
so to fix the above issue we need to link the required library, here cos() and sin() are present in the math library so we need to link math library with the program as follows
gcc mat1.c -lm
-lm will link the math library with above program so program build properly, so as like this gcc has few other library needs to link(but not limited to) are posix threads (-lpthread), pipes (-pipe), please check documentation for details
this program will compile but linker fails to find definition of cos and sin functions
#include<stdio.h>
#include<math.h>
#include <stdlib.h>
#define PI 3.142
int main()
{
double sinval, cosval;
int x=2;
sinval = sin(PI/x);
cosval = cos(PI/x);
printf("sin and cos of PI/x is %lf %lf\n", sinval, cosval);
return 0;
}
followings link errors we will hit, since linker can't able to resolve the definition of cos() and sin()
shiv@ubuntu:~$ gcc mat1.c
/tmp/ccHOvWsZ.o: In function `main':
mat1.c:(.text+0x2a): undefined reference to `sin'
mat1.c:(.text+0x4b): undefined reference to `cos'
collect2: ld returned 1 exit status
so to fix the above issue we need to link the required library, here cos() and sin() are present in the math library so we need to link math library with the program as follows
gcc mat1.c -lm
-lm will link the math library with above program so program build properly, so as like this gcc has few other library needs to link(but not limited to) are posix threads (-lpthread), pipes (-pipe), please check documentation for details
Comments
Post a Comment