c语言 使用gcc调试 有问题

来源:百度知道 编辑:UC知道 时间:2024/05/24 20:13:56
#include <math.h>
#include <stdio.h>
main()
{
int a=2;
printf("%f",sqrt(2));
// printf("%f",sqrt(a));
}
注释那行报错
/tmp/ccQiDW12.o: In function `main':
test.c:(.text+0x4e): undefined reference to `sqrt'
collect2: ld 返回 1
上面正常 tc下都正常,这是为什么呢?

编译时加上选项 -lm

假设你程序文件为soure.c
gcc -lm -Wall source.c

我这里没问题.....

从你报错的地方看,sqrt没有定义,你看一下你的gcc库是不是不全。

因为你在main前面少了东西啊!
你可以用void main()或者比如你想返回个int型的数据,那你就用int main(),这样就可以了啊

#include <stdlib.h>

因为sqrt的参数是浮点型,你传的是整型,可能是这样吧