gpt4 book ai didi

c - C 中的平方根程序未编译

转载 作者:太空宇宙 更新时间:2023-11-04 06:50:14 25 4
gpt4 key购买 nike

<分区>

刚开始学C,在C教程网站上找到这个例子程序,编译时报错。

这是程序,根据用户输入计算数字的平方根:

#include <stdio.h>
#include <math.h>

int main()
{
double num, root;

/* Input a number from user */
printf("Enter any number to find square root: ");
scanf("%lf", &num);

/* Calculate square root of num */
root = sqrt(num);

/* Print the resultant value */
printf("Square root of %.2lf = %.2lf", num, root);

return 0;
}

我在 Ubuntu 中使用 gcc 编译它:

gcc -o square_root square_root.c

这里是错误:

/tmp/cc9Z3NCn.o: In function `main':
square_root.c:(.text+0x4e): undefined reference to `sqrt'
collect2: error: ld returned 1 exit status

我做错了什么?我可以看到导入了 math 模块,为什么会出错?

再说一次,我今天刚开始学C,只想搞清楚怎么让程序跑起来。感谢您的耐心等待,因为这一定是显而易见的事情。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com