“A young man is smoking one cigarette after each other without a pause. An elderly woman observes that and says: “Young man, you are smoking like crazy! Don’t you know that there is a warning on each cigarette package that this can kill you?” The young man finishes his cigarette, looks at the elderly person and says: “Yes, I know. But look, I’m a programmer, and it is only a warning.”
I don’t smoke, and I do pay attention to warnings :-). I always try to keep my source code free of compiler warnings. And I always pay special attention to the following on:
So what does the gcc warning ‘implicit declaration of function’ mean (other compiler report a ‘implicit parameter declaration’)? Basically it means that the compiler has found a call to function for which he does not have a prototype. For the case it missed a declaration like this:
errorCode_t calcMatrix(matrix_t *m, double weight);
Either because I did not provide a ‘forward declaration’ of it or I missed to include the header file with that declaration, or that declaration is missing in the header file.
Without knowing the interface, the compiler has to assume (implicitly) the interface from the actual parameters used, and that the function returns an
int. So the compiler has to assume according to the rules:
int calcMatrix(int*, int);
Which of course is wrong in many ways: not only the parameter
3 is not converted to a double, it means that the wrong size (
double) is assumed. If that parameter is passed on the stack it will result in a stack space mismatch, so overall very bad. It might ‘work’ if the types by chance match the actual implementation or will be less of a runtime problem if parameters and return values are passed in registers. Still, it is clearly wrong and bad, bad, bad. It might be only a warning if the linker does not find that symbol (
calcMatrix in this case) somewhere, so if you do not pay attention to warnings, your application might crash or behave in wrong way.
Such an ‘implicit declaration’ is really an oversight or error by the programmer, because the C compiler needs to know about the types of the parameters and return value to correctly allocate them on the stack. Unfortunately in C this is not an error but a warning (for legacy reasons, to be able to compile old non-compliant code). The good news is that compiling such a thing with in C++ will produce an error, another good reason to compile plain C code with C++ mode.
The other good news is that every C compiler usually has an option to turn this warning into an error (what I recommend if you are not paying attention to warnings). From the gcc help page:
-Werror-implicit-function-declarationGive a warning (or error) whenever a function is used before being declared. The form -Wno-error-implicit-function-declaration is not supported. This warning is enabled by -Wall (as a warning, not an error).
With this option added to the compiler settings it gets flagged as an error:
Happy warning 🙂
- GNU gcc warning options: Warning Options – Using the GNU Compiler Collection (GCC)