GCC:undefined reference to ‘sqrt’

2016-1-13 杜世伟 C

用c语言求一个小于100000的一个整数,要求这个数加上100后是一个完整平方数,在加上268又是一个完整平方数,请问该数有哪些?

#include <stdio.h>
#include <math.h>
main(){
long int i,x,y;
for(i=1;i<100000;i++){
x = sqrt(i+100);
y = sqrt(i+268);
if(x*x == i+100 && y*y == i+268){
printf("\n%ld\n",i);
}
}
}

将以上代码保存成test02.c
通过gcc test02.c -o test02 执行的时候提示


20160113111825.png
undefined reference to `sqrt'
代码中是我是引入math.h类库的,提示没有找到!该问题为实际上没有链接math库,编译时需要加上 -lm 参数(链接math库)
通过执行 gcc -lm test02.c -o test02即可


QQ截图20160113192837.png


标签: gcc sqrt

Powered by emlog 沪ICP备2023034538号-1