你可能认为我在编程时非常疯狂而且非常糟糕.其中一个可能是这种情况,但请阅读我的发现.
是的,我 #include <math.h>
完整代码可以在这里找到.(我试图让它符合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
Run Code Online (Sandbox Code Playgroud)
现在使用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
Run Code Online (Sandbox Code Playgroud)
使用c99标准时,程序的行为符合要求和预期.
为什么使用-ansi标志似乎会从math.h中删除声明?