When you make the typical C program that uses IO using the stdio.h
header file. Why is it that the header file doesnt need to be linked
with the -l argument, but when you are including a library from say a
tutorial or such you also have to name the file in your compile
command.
This is a characteristic not of the C language, but of the C
implementation you're using. The rules of the language tell you
how to express what the program should do, and that's "the same"
(with some permitted variations) for all implementations. But the
incantations that you use to tell the implementation to translate
your source files, combine them into a program, find necessary
libraries, and run the result are the business of the implementation,
and implementations differ. Sometimes they differ a lot.
Most C implementations (all that I've run into) will find at
least some of the Standard library routines without being told
explicitly where to look for them. This is done as a convenience
for the programmer: Since a very large fraction of C programs will
use the things declared in <stdio.h> and <stdlib.h> and <string.h>
and so on, the implementation "just knows" where to look for them
and will do so "on its own."
For historical reasons, some implementations treat the <math.h>
functions differently: They're found in a conventional place, but
the place won't be searched unless you ask for it with something
like an "-lm" flag. Others treat the <math.h> functions the same
way they treat the <ctype.h> functions, and will find them without
being tickled.
No implementation I know of will automatically search user-
defined libraries without being told to do so, although I suppose
it's possible. The problem (from the implementation's perspective)
is that there might be several user-defined libraries lying around,
perhaps containing several variant versions of the same functions;
how is the implementation to know which library it should search?
Also, for some implementations the search order is important: If
your program calls libFunc() and libFunc() calls atan2(), it might
be important to search the math library after rather than before
searching the library that contains libFunc(). Again, this is a
problem for the implementation: How is it supposed to figure out
which of the N! ways to search N libraries is the right one? So
implementations usually require you to specify (somehow) which of
your own libraries to use and what order to search them in.
Is it something to do with the Standard Library already being linked
in with the compiler. If that is the case why do you have to include
it in the source file. I guess I could take another stab at it and
guess that it is because the object code is already linked in by
default for the Standard library. I don't think it is as easy as the
linker not knowing the PATH to the header file.
The headers like <stdio.h> and <string.h> -- and for that matter
"mystuff.h" -- don't actually contain the functions printf() and
strtok() and myFunc(), but only descriptions of those functions.
"Listen up, compiler," says <stdio.h>, "I'm going to make some calls
to a function named fgets(), which takes three parameters of these
types and returns a value of that type." With this information the
compiler knows what kind of code to generate when it sees an fgets()
call in your source code, but it still doesn't know what the body of
fgets() looks like. It goes ahead and translates your source code,
accepting on faith that it will eventually find the compiled guts of
an fgets() function, somewhere. You get an object file containing
"external references" to functions (and sometimes data) that will be
found elsewhere at link time.
That's the "classical" style, anyhow. Some compilers do in fact
have built-in knowledge of Standard library functions, especially
short functions that might compile to in-line instructions instead of
to full-strength function calls. A call to sqrt(), for example, might
turn into an FSQRT instruction right in the middle of your code, with
no external reference to a separate sqrt() function and no need to
look for one in a library. But these are departures from the norm
for the sake of optimization; for most functions (both the Standard
library's and your own), the headers merely declare and the function
bodies are obtained from somewhere else.