Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does gcc -ansi remove the declarations of cosf and the like?

Tags:

c

gcc

c99

c89

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 ?

like image 940
EnabrenTane Avatar asked Dec 04 '25 08:12

EnabrenTane


2 Answers

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.

like image 104
ismail Avatar answered Dec 05 '25 22:12

ismail


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.

like image 35
R.. GitHub STOP HELPING ICE Avatar answered Dec 05 '25 22:12

R.. GitHub STOP HELPING ICE