编译和执行时出现c编程问题错误

编译和执行时出现c编程问题错误,c,C,我试图编译和执行代码时出错 源代码: #include <stdio.h> #include <math.h> #define PI 3.14125365 #define MAX 180 main() { int angle; float x,y; angle = 0; printf("Angle Cos(angle)\n"); while(angle <= MAX) { x = (PI/MAX)*a

我试图编译和执行代码时出错

源代码:

#include <stdio.h>
#include <math.h>
#define PI 3.14125365
#define MAX 180

main()
{
    int angle;
    float x,y;
    angle = 0;
    printf("Angle Cos(angle)\n");
    while(angle <= MAX)
    {
        x = (PI/MAX)*angle;
        y = cos(x);
        printf("%5d %3.4f\n", angle,y);
        angle = angle + 10;
    }
}
在尝试编译之后,我还收到一条警告消息和错误消息,如下所示

math_functions.c:6:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
 main()
 ^~~~
/tmp/ccOZSgOb.o: In function `main':
math_functions.c:(.text+0x3d): undefined reference to `cos'
collect2: error: ld returned 1 exit status

在第一种情况下,您试图将C源文件作为shell脚本运行。这不是运行C程序的方式。首先编译程序并运行生成的可执行文件

出现编译错误的原因是您使用的是
cos
函数,该函数位于默认情况下未链接的数学库中。您需要在编译命令的末尾添加
-lm
,以便在中链接该库

gcc -g -Wall -Wextra -o math_functions math_functions.c -lm
另外,您得到返回类型默认为
int
的警告的原因是您的主函数没有指定的返回类型。在声明前加一个int,你就没事了

int main()

重新开放。标记的dup只针对问题的前半部分,而不是后半部分。
int main()