Why does LLVM complain about missing function prototypes?
LLVM 2.1 has an option that enables warnings for "missing function prototypes." When enabled, the warning will complain about a file like this:
double square( double d )
{
return d*d;
}
void main()
{
// ...
}
The function "square" will trigger a warning because it is defined without having been declared (prototyped). You can eliminate the warning thus:
double square( double d );
double square( double d )
{
return d*d;
}
void main()
{
// ...
}
I've programmed in C++ for twenty years and I've never seen a warning like this. It does not seem useful to me.
By default, this warning is enabled in new Mac console projects (at least) in Xcode 4.1. Evidently someone found it useful enough to first implement 开发者_开发技巧it and then enable it by default.
Why is this a useful warning? Why does LLVM have it as an option? Why is the option enabled by default on Xcode?
The compiler uses the prototype declaration to match types for the function definition.
If you are writing the prototype in a header(interface) file and the implementation in the source file then this warning (by forcing you to provide a declaration, effectively) would prevent you from making a typo error where function definition is different than the one in declaration.
Though, without such an warning, you would get the errors while linking. One might end up wondering what the actual problem is(n number of reasons for linking errors).
The warning during compilation stage is much better indication of error than a linking error.
If could be useful to make sure every function is either visible from some header, or static.
I had cases where two files were linked together, even though none of them used the same header file.
Take this example:
int test()
{
return 0;
}
If there's no header, you can have a second file which does:
extern int test();
test();
If you are writing a library, this warning could tell you that someone could be using this function even if they were not supposed to, since this function is in no header. They should have been marked as static.
Prototype of "int test()" should be "int test(void)" then it's OK.
精彩评论