(e-mail address removed) a écrit :
Hi Everyone,
int main()
{
printf("not included stdio.h");
}
Yes, i haven't included stdio.h and my compiler would generate a
warning and would assume that it would return a int, my question is how
does the linker manage to link the function invocation to the proper
printf function?
and does including a header file having function prototype help the
linker in any way?
1) Linkers deal with object files, this has nothing to do with header
files.
2) Header files describe interfaces of functions and modules. This has
nothing to do with object files.
Please keep those apart.
Now, the interfaces descriptions are for the compiler, that generates
code according to those descriptions.
When (in C ) a prototype is not in scope, an automatic
prototype is provided by the compiler. It assumes a function that
has an undertemined number of arguments and returns an int.
In both cases, whether a prototype is in scope or not,
an external reference to a function is issued by the compiler.
In this case the object code would contain an external
reference to the function "printf" (or "_printf", it depends
on the compiler).
The linker goes through the object files and sees:
"Mmmm this is an external reference to the printf function.
Let's look if I find it somewhere".
Then, depending on the linker, it will find it or not in the
libraries it uses BY DEFAULT.
Default libraries vary from compiler system to compiler system.
At least the startup code is ALWAYS automatically included,
then, most compilers assume the C library as a default
library, i;e. one that you do not need to include in the
linker command line. Other compilers may differ, for example some
braindead compiler systems force you to write the math
library as an extra command line option, and it is not
included by default, so if you use the sqrt function for
instance, compilation will fail unless you tell the linker to
add the math library.
Others will include a lot of default libraries so that your
code mostly will link without specifying any extra library
Microsoft Visual C for instance will include all these:
comctl32.lib shlwapi.lib kernel32.lib user32.lib gdi32.lib
winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib
oleaut32.lib uuid.lib odbc32.lib odbccp32.lib
Other compiler systems will be less bloated, for instance
lcc-win32 includes just 6-7 libraries.
Then, the answer to your question is simple:
The linker finds the printf function in the C library of
the compiler that is included by default.
jacob