为什么 gcc -ansi 删除 cosf 等声明?
你可能认为我完全疯了,并且编程非常糟糕。其中之一可能是这种情况,但请阅读我的发现。
是的,我 #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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您查看
GCC 内置文档
,您将看到C99
标准中引入了sinf
和cosf
函数(以及许多其他相关函数)。If you check the
GCC Builtins documentation
, you'll see thatsinf
andcosf
functions (and many more related ones) are introduced in theC99
standard.不要将
-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.