Re: Include statements and compiler linking



On May 6, 11:33 pm, Joe Hacker <joehacke...@xxxxxxxxx> wrote:
I have what is probably a simple question for some but the process
escapes my understanding. I am a newbee c programmer and understand
some of the basics but I usually just blindly follow directions.
Unfortunatly I'm not the type of person that just does things without
wantint to understand why I have to do certain things.

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.

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.

I appreciate any help guys and look forward to be a active member of
the list.

Thanks

[File all of this under "Lies to programmers",
i.e. it is not true, but it is a reasonable approximation]

The implementation works more or less like this

-you provide some c code to do something useful

-there is function (e.g printf), that has been written by
someone else, (they may or may not have used c)
instead of writing this yourself you want to just
use it. You tell the compiler how to use the code
by including a header file

-the compiler will then create code that correctly
calls printf, but will not include the code
for printf

-the linker will create an executable by taking the
code provided by the compiler and adding the code for
printf. You can tell it where to find this code by
adding an -lm option.

Now, usually the implementation will not include the header
files for common functions by default, but will search
for the code to common functions by default. So if you want to
use printf you have to #include stdio.h to tell the compiler how
to call printf, but you do not have to -lm libc.h to tell the linker
where to find the code for printf.

Why the difference? Well, there are many common functions
so if you simply #include the instructions to call any
of them, you might make a very small source file very big.
A simple approach is to scan the file
first to find out which functions
are going to be used, then go back and start again, but
this would take time so it was not done by the first
compilers. On the other hand, by the time the linker
gets to things, you know which functions you want, so the linker
only has to put the stuff you need in.

[Could a compiler be written so as to recognize
many common functions, and automatically include the needed
files? Yes. Is this done? No. There are explicit rules about
what should be done if a header file for a function is not
included, and including the right header file could do something
else. There is nothing to stop the compiler warning you that
you are telling it to do something that is almost certainly not
what you want.]

Now, why can't the linker find every function. Well, there
are an infinite number a possible functions, and the code
could be anywhere, so the linker would have to do a very
big search. Worse, the linker would often find more than
one function with the same name. How would it know which
one to use? So if you want to use e.g. a tutorial function,
you have to tell the linker where to find it.

- William Hughes

.



Relevant Pages

  • Re: header files
    ... As all code can be written in the header file ... > void foo() ... > compiler tries to compile main.c it eventually reaches the line ... > the compiler has never heared about a function called printf(). ...
    (alt.comp.lang.learn.c-cpp)
  • Re: header files
    ... As all code can be written in the header file ... void foo() ... So when the compiler compiles ... the compiler has never heared about a function called printf(). ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Quines
    ... No header file is included and the code works (Compiled ... assumed that printf() is an ordinary function, not a variadic one, ... source code file fed to the compiler. ...
    (comp.lang.c)
  • Re: Function is not recognizing
    ... Specifying a header file gives information to the compiler ... At LINK time, the linker needs to know the name of the .obj or .lib file ... What you are doing wrong is failing to tell the LINKER where to find the CODE; ... sufficient to tell the COMPILER where to find the HEADER FILES. ...
    (microsoft.public.vc.mfc)
  • Re: Quines
    ... No header file is included and the code works (Compiled ... assumed that printf() is an ordinary function, not a variadic one, ... source code file fed to the compiler. ...
    (comp.lang.c)