【发布时间】:2019-03-05 13:07:12
【问题描述】:
刚开始学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,我只是想弄清楚如何让程序运行。感谢您的耐心等待,因为它必须是显而易见的。
【问题讨论】:
-
程序运行良好。我认为编译器存在一些问题。您可以尝试在 ideone 等在线 IDE 上编译您的程序。它会起作用的。
标签: c compiler-errors square-root