Linking object files, making include redundant?
I am trying my hand at a bit of C. And I thought I had understood this linking business. But I guess not. I have a simple file main.c:
#include "function.h"
int main(char args[])
{
int print = myfunction();
}
then a second pair of files function.c/function.h
int myfunction(); //function.h
int my开发者_如何学编程function() //function.c
{
return 5;
}
compiling this works great. However, it works great regardless, whether I use #include "function.h"
in my main file or not. Why would I need to include function.h then?
I don't know what system you are on, but if it uses gcc or something compatible, try it again with
cc -Wall -Werror *.c
Or, turn on the equivalent options for your system.
Large programs are always built this way, so that the compiler checks the argument types. Unlike the dynamic and scripting languages, C generates actual machine code and does not check for parameter count or compatibility at runtime.
So, the function prototypes were added to the base language to do type checking at compile time. They are optional.
A C compiler doesn't require that you specify the prototype for a function1 before you use it. The prototype just lets the compiler verify that the type(s) of parameter(s) you pass fit with the type(s) the function requires -- and implicitly convert to the right type if it's not right, and there is an implicit conversion from/to the types involved.
As long as your code is perfect, and there's no mismatch between the how you use a function and how that function was intended to be used, you won't have a problem. In your test, you have a function that takes no parameters, and returns an int, and the code that uses it does essentially nothing else. That's a situation that's pretty hard to screw up, and it works just fine. In a real program with hundreds or thousands of functions taking multiple parameters of complex types, etc., the situation changes pretty quickly. Letting the compiler assure that you're calling functions correctly becomes much more important.
1 With the exception of a variadic function, and even there the "variable" parameters still basically follow the same rules as if there was no prototype for the function.
When you use a function that has not been defined, many C compilers will just assume that it's an extern function returning an int (you will often get a warning, but the code will compile.) You will run into problems when you start using functions with more complex arguments and return types, though.
If you use a function that has not been declared, older C compilers will assume it has arguments ...
and return type int
. This is not recommended. If you turn up compiler warnings, you'll likely get a warning about using an undeclared function. The compiler can catch more mistakes if it sees function.h before compiling main.c so it knows exactly how myfunction
is supposed to act.
You declare a function within a headerfile to use it in different modules. You don't necessarily need the source code. You can link against binary libraries. During compile time you need a declaration of the method for checking arguments,types etc.
In your simple example this wouldn't be required.
精彩评论