使用GCC将Math库链接到C90代码

And*_*ree 1 gcc math.h c89

我想用数学库编译一个简单的C90代码:

main.c中:

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

int main()
{
  printf("M_PI: %f\n", M_PI);
}
Run Code Online (Sandbox Code Playgroud)

我使用GCC编译器并使用选项-ansi -pedantic来强制执行C90标准.

gcc -ansi -pedantic -lm main.c
Run Code Online (Sandbox Code Playgroud)

但它没有编译.以下是错误消息:

main.c: In function ‘main’:
main.c:7:25: error: ‘M_PI’ undeclared (first use in this function)
main.c:7:25: note: each undeclared identifier is reported only once for each function it appears in
Run Code Online (Sandbox Code Playgroud)

我的问题是,为什么?C90标准是否禁止使用数学库?

Dar*_*ner 5

当需要严格的iso标准时,不定义M_PI.在三角函数下查看页面.建议在使用-ansi时,只需自己定义:

#define M_PI 3.14159265358979323846264338327
Run Code Online (Sandbox Code Playgroud)