为什么 gcc -ansi 删除 cosf 等声明?

发布于 2024-10-10 06:23:19 字数 2859 浏览 2 评论 0原文

你可能认为我完全疯了,并且编程非常糟糕。其中之一可能是这种情况,但请阅读我的发现。

是的,我 #include

完整代码可以在此处找到.( 我试图使其兼容 ansi 以便在 VS2010 上编译,但它出现了有关混合代码和声明的错误,并且缺少 fminf()。令我惊讶的是 VS2010 关心混合代码和声明默认警告级别。我记得 2008 年不关心,但可能是错误的)

这是使用 c89/-ansi 标准时的 gcc 输出。注意函数的隐式声明。还有一些其他关于未使用的参数,但我们现在不关心这些。 (需要签名来注册 GLUT 回调)

当我使用 c89 或 ansi 标准运行应用程序时,它会产生错误的输出,就像数学函数没有按预期运行一样。

$ STANDARD=-std=c89 make -f Makefile.Unix
gcc -std=c89 -Wextra -Wall -pedantic  -c -o file-util.o file-util.c -I/usr/X11R6/include
gcc -std=c89 -Wextra -Wall -pedantic  -c -o gl-util.o gl-util.c -I/usr/X11R6/include
gcc -std=c89 -Wextra -Wall -pedantic  -c -o meshes.o meshes.c -I/usr/X11R6/include
In file included from meshes.c:12:
vec-util.h: In function ‘vec_length’:
vec-util.h:10: warning: implicit declaration of function ‘sqrtf’
meshes.c: In function ‘calculate_flag_vertex’:
meshes.c:48: warning: implicit declaration of function ‘sinf’
meshes.c:50: warning: implicit declaration of function ‘cosf’
gcc -std=c89 -Wextra -Wall -pedantic  -c -o flag.o flag.c -I/usr/X11R6/include
In file included from flag.c:18:
vec-util.h: In function ‘vec_length’:
vec-util.h:10: warning: implicit declaration of function ‘sqrtf’
flag.c: In function ‘update_p_matrix’:
flag.c:58: warning: implicit declaration of function ‘fminf’
flag.c: In function ‘mouse’:
flag.c:252: warning: unused parameter ‘x’
flag.c:252: warning: unused parameter ‘y’
flag.c: In function ‘keyboard’:
flag.c:261: warning: unused parameter ‘x’
flag.c:261: warning: unused parameter ‘y’
flag.c: At top level:
vec-util.h:1: warning: ‘vec_cross’ defined but not used
vec-util.h:13: warning: ‘vec_normalize’ defined but not used
gcc -o flag file-util.o gl-util.o meshes.o flag.o -L/usr/X11R6/lib -lGL -lglut -lGLEW

现在使用c99 标准中函数消息的隐式声明消失了。

$ STANDARD=-std=c99 make -f Makefile.Unix
gcc -std=c99 -Wextra -Wall -pedantic  -c -o file-util.o file-util.c -I/usr/X11R6/include
gcc -std=c99 -Wextra -Wall -pedantic  -c -o gl-util.o gl-util.c -I/usr/X11R6/include
gcc -std=c99 -Wextra -Wall -pedantic  -c -o meshes.o meshes.c -I/usr/X11R6/include
gcc -std=c99 -Wextra -Wall -pedantic  -c -o flag.o flag.c -I/usr/X11R6/include
flag.c: In function ‘mouse’:
flag.c:252: warning: unused parameter ‘x’
flag.c:252: warning: unused parameter ‘y’
flag.c: In function ‘keyboard’:
flag.c:261: warning: unused parameter ‘x’
flag.c:261: warning: unused parameter ‘y’
flag.c: At top level:
vec-util.h:1: warning: ‘vec_cross’ defined but not used
vec-util.h:13: warning: ‘vec_normalize’ defined but not used
gcc -o flag file-util.o gl-util.o meshes.o flag.o -L/usr/X11R6/lib -lGL -lglut -lGLEW

当使用 c99 标准时,程序的行为符合预期。

问题

为什么使用 -ansi 标志似乎会从 math.h 中删除声明?

You probably think I am completely crazy and terribly bad at programming. One of those may be the case, but please read my findings.

Yes, I #include <math.h>

Full Code can be found here.( I was trying to make it ansi compliant to get it to compile on VS2010, It through an error about mixed code and declaration, and fminf() missing. I was surprised that VS2010 cared about mixed code and declaration with default warning levels. I recall 2008 not caring, but could be wrong. )

Here is the gcc output when using the c89/-ansi standard. note the implicit declarations of functions. There are a few others about unused parameters, but we don't care about those for now. ( needed for signature to register call backs with GLUT)

When I run the application using the c89 or ansi standard, it produces the wrong output, much like the math functions are not behaving as expected.

$ STANDARD=-std=c89 make -f Makefile.Unix
gcc -std=c89 -Wextra -Wall -pedantic  -c -o file-util.o file-util.c -I/usr/X11R6/include
gcc -std=c89 -Wextra -Wall -pedantic  -c -o gl-util.o gl-util.c -I/usr/X11R6/include
gcc -std=c89 -Wextra -Wall -pedantic  -c -o meshes.o meshes.c -I/usr/X11R6/include
In file included from meshes.c:12:
vec-util.h: In function ‘vec_length’:
vec-util.h:10: warning: implicit declaration of function ‘sqrtf’
meshes.c: In function ‘calculate_flag_vertex’:
meshes.c:48: warning: implicit declaration of function ‘sinf’
meshes.c:50: warning: implicit declaration of function ‘cosf’
gcc -std=c89 -Wextra -Wall -pedantic  -c -o flag.o flag.c -I/usr/X11R6/include
In file included from flag.c:18:
vec-util.h: In function ‘vec_length’:
vec-util.h:10: warning: implicit declaration of function ‘sqrtf’
flag.c: In function ‘update_p_matrix’:
flag.c:58: warning: implicit declaration of function ‘fminf’
flag.c: In function ‘mouse’:
flag.c:252: warning: unused parameter ‘x’
flag.c:252: warning: unused parameter ‘y’
flag.c: In function ‘keyboard’:
flag.c:261: warning: unused parameter ‘x’
flag.c:261: warning: unused parameter ‘y’
flag.c: At top level:
vec-util.h:1: warning: ‘vec_cross’ defined but not used
vec-util.h:13: warning: ‘vec_normalize’ defined but not used
gcc -o flag file-util.o gl-util.o meshes.o flag.o -L/usr/X11R6/lib -lGL -lglut -lGLEW

Now using the c99 standard the implicit declaration of function messages are gone.

$ STANDARD=-std=c99 make -f Makefile.Unix
gcc -std=c99 -Wextra -Wall -pedantic  -c -o file-util.o file-util.c -I/usr/X11R6/include
gcc -std=c99 -Wextra -Wall -pedantic  -c -o gl-util.o gl-util.c -I/usr/X11R6/include
gcc -std=c99 -Wextra -Wall -pedantic  -c -o meshes.o meshes.c -I/usr/X11R6/include
gcc -std=c99 -Wextra -Wall -pedantic  -c -o flag.o flag.c -I/usr/X11R6/include
flag.c: In function ‘mouse’:
flag.c:252: warning: unused parameter ‘x’
flag.c:252: warning: unused parameter ‘y’
flag.c: In function ‘keyboard’:
flag.c:261: warning: unused parameter ‘x’
flag.c:261: warning: unused parameter ‘y’
flag.c: At top level:
vec-util.h:1: warning: ‘vec_cross’ defined but not used
vec-util.h:13: warning: ‘vec_normalize’ defined but not used
gcc -o flag file-util.o gl-util.o meshes.o flag.o -L/usr/X11R6/lib -lGL -lglut -lGLEW

When using the c99 standard the program behaves as desired and expected.

The Question

Why would using the -ansi flag seemingly remove the declarations from math.h ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

呆橘 2024-10-17 06:23:19

如果您查看 GCC 内置文档,您将看到 C99 标准中引入了 sinfcosf 函数(以及许多其他相关函数)。

If you check the GCC Builtins documentation, you'll see that sinf and cosf functions (and many more related ones) are introduced in the C99 standard.

不再见 2024-10-17 06:23:19

不要将 -ansi 用于现代代码。尽管 ANSI C 的当前版本与 ISO9899-1999 (C99) 保持一致,但 -ansi 已被 gcc 永久指定为“旧模式”。如果您正在编译 C99 代码,只需使用 -std=c99 即可。这是现代的等价物。

Don't use -ansi for modern code. Despite the current version of ANSI C being aligned with ISO9899-1999 (C99), -ansi has been permanently assigned to mean "legacy mode" by gcc. Just use -std=c99 if you're compiling C99 code. It's the modern equivalent.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文