在 VS Code 中使用 pow 函数时显示运行代码错误

问题描述 投票:0回答:1
当我要使用 VS Code 的 pow 函数终端运行一个简单的代码时显示错误。本质上,我必须编写一个程序,除其他外,计算给定边的正方形面积。我以为我应该使用 pow() 函数而不是简单地使用 side*side,但是编译器一直给我以下错误:

/usr/bin/ld: /tmp/ccEMiotv.o: in function `main': area_square.c:(.text+0x4d): undefined reference to `pow' collect2: error: ld returned 1 exit status
我的代码如下所示:

#include<stdio.h> #include<math.h> int main() { //calculate area of a square double side, area; printf("Enter the value of side of square: "); scanf("%.2lf", &side); area = pow(side,2); printf("The area of square is %.2lf", area); return 0; }
我正在使用命令进行编译

gcc area_square.c

我能做些什么来解决这个问题,请给我一些建议。任何想法将不胜感激!

c clang
1个回答
0
投票
gcc -o area_square area_square.c -lm
尝试使用上面的命令进行编译,因为它链接了数学库。
在 scanf 中也使用 %lf 代替 %.2lf

© www.soinside.com 2019 - 2024. All rights reserved.