环境:
1、ubuntu 16.04
2、Vim 7.4
问题:
1、调用math.h头文件中的函数,直接按如下编译会报错。
//
gcc test.c
//
//
/tmp/ccJKFWzl.o: In function `main':
1.c:(.text+0x45): undefined reference to `sin'
collect2: error: ld returned 1 exit status
//
//
#include <math.h>
#include <stdio.h>
int main()
{
double x, r;
printf("Please input a number\n");
scanf("%lf", &x);
r = sin(x);
printf("%lf", r);
}
//
解决方案:
//
在编译时加上-lm即可,表示告诉编译器到libm.so库文件中找这个函数,通常在/lib中。
gcc test.c -lm
//