I am a bit confused why this code compiles. I leave out the ‘necessary’ #include <OpenGL/gl.h> and still the program can compile. How is this possible when my program is calling functions from the GL library, without including them.
int main(int argc, char** argv) { glClearColor(1.0,1.0,1.0,1.0); return 0; }
I use this compilation command:
gcc -framework GLUT -framework OpenGL test.c
I was under the assumption that adding -framework just specifies to the linker where the library is, but I thought I still need the headers?
In C (prior to C99), you don’t have to declare functions in order to use them. The compiler will assume the function takes the promoted parameter types you pass to it, and assume the function returns an
int. This, however can be quite problematic, and behavior is undefined if the function doesn’t. Let’s look at this:Because the types are being promoted (
char -> int, float -> double) if there is no declaration of the function at the time you call it, the arguments could not be passed at the right places in memory anymore. Accessing b can yield to a curious value of the parameter. As a side node, the same problem occurs when you pass arguments tovararg functionslikeprinft, or functions having no prototype (likevoid f(), where there is no information about the parameters types and count). This is the reason that you always have to access variadic arguments withva_argusing their promoted type. GCC will warn you if you don’t.Always include the proper header files, so you don’t run into this problems.
Edit: thanks to Chris for pointing out that
char literals(like'a') are always of typeintin C