使用 GCC 将数学库链接到 C90 代码
我想利用数学库编译一个简单的 C90 代码:
main.c:
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
int main()
{
printf("M_PI: %f\n", M_PI);
}
我使用 GCC 编译器并使用选项 -ansi -pedantic 来强制执行 C90 标准。
gcc -ansi -pedantic -lm main.c
但它无法编译。以下是错误信息:
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
我的问题是,为什么? C90标准是否禁止使用数学库?
I would like to compile a simple C90 code utilizing math library:
main.c:
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
int main()
{
printf("M_PI: %f\n", M_PI);
}
I use GCC compiler and use option -ansi -pedantic to enforce C90 standard.
gcc -ansi -pedantic -lm main.c
But it doesn't compile. The following is the error message:
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
My question is, why? Does C90 standard prohibits the use of math library?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当需要严格的 ISO 标准时,M_PI 未定义。查看三角函数下的此页面。建议使用-ansi时,自己定义即可:
M_PI isn't defined when strict iso standard is required. Look on this page under trigonometric functions. It is suggested that when using -ansi, just define it yourself:
M_PI 通常被声明为宏,并且有一个显式保护
#if !define(_ANSI_SOURCE)
(至少在 OSX 中),这表明 ANSI 实现不支持gcc,您也可以使用 < code>-std=c90 强制 C90
M_PI is generally declared as a macro and there is an explicit guard
#if !defined(_ANSI_SOURCE)
(at least in OSX), which suggests that the ANSI implementation doesnt support itfor gcc you can also use
-std=c90
to force C90