Re: How do header files work?

From: Luther Baker (lutherbaker_at_yahoo.com)
Date: 05/19/04


Date: 19 May 2004 13:24:15 -0700

matthew_hurne@yahoo.com (matthurne) wrote in message news:<4bdff256.0405190650.abf00c0@posting.google.com>...
...
> specifically. Basically, I don't understand how a header file being
> included makes a connection with the source file that has the
> definitions for whatever is in the header file. It is my
...

Its a complicated process of which I do not have a lot of experience,
so I'm sure I will misspeak, but imagine the original implementers
writing the standard libraries. For simplicity, let's say they write
"something.h" that declares lots of things and "something.cpp" that
defines or implements lots of things.

Well, many commercial vendors don't want to give away their
implementation code, so they compile the implementation
"something.cpp" into binary, operating system specifc object files or
libraries - and distribute the libraries or object files with their
associated header files - ie: the implementation source code
"something.cpp" is not needed.

Now, as a developer, when you want to use functionality from these
files - you must include "something.h" in your source code - so that
during the first phase of compilation, amongst other things, your
compiler can look for syntax problems .. checking legal use of
functions, structures, classes, etc. against the declarations in the
header file.

Then you reach a second stage of compilation. When you actually
compile and create your own object files or executables, the compiler
searchs library paths, accessing its libraries and linking them into
your code. At this point, if object files or libraries that are linked
in don't somehow match what their respective header files said - then
lots of things will go terribly wrong. Hopefully, this never happens.

Man is this ever simplistic, but:

1. The compiler looks through your code and validates your syntax with
the syntax in the included header files
2. Then, in a different phase, the compiler locates libraries and
either links them to your target or statically compiles code into your
target object file or executable.

The names of the header files are irrelevant. As long as you've
inluded the proper names in your source code - and valid object files
or libraries implementing those header files are in the compiler's
library path, all will compile just fine.

As a side note, some library's header files are located in the
standard include area and are therefore easily seen via #include
<myheader.h> but to compile your code, you are forced to add a flag or
some other indicator to the compiler to tell it where or which library
to use.

   g++ -o myapp.exe myapp.cpp -lthirpartylib

Hth,

-Luther



Relevant Pages

  • Re: Borland C++BuilderX
    ... Mostly, programmers do not worry about the contents of header files, ... the rest of the source code that I provide but use the full name when I ... The next two lines of our program are instructions to the compiler to ... object code files and libraries. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: About libraries and headers
    ... Libraries aren't a C concept, they are an operating system concept. ... Borland compiler has a facility to "pre-compile" header files where it ... typedef double MyType; ...
    (comp.lang.c)
  • Re: changing module files
    ... my previous post (Will Fortran 2003 Solve This Problem). ... C++ header files avoid that problem, because header files only declare the ... INTERFACE to class member functions. ... > Just because the implemention of the compiler is not in a standard does ...
    (comp.lang.fortran)
  • Re: Beware of /Zp1 compiler option when compiling for X64
    ... This problems are not isolated to X64 compiles. ... Looking at the latest SDK header files and the Feb 2003 version that I had ... I am going to follow your advice about protecting the Windows SDK header ... Eventually I realised that the problem was caused by the compiler options ...
    (microsoft.public.win32.programmer.kernel)
  • Re: msvcr71.dll necessary? - Re: Which compiler will Python 2.5 / Windows (Intel) be built with
    ... Windows Update Process. ... you need the corresponding set of header files and libraries. ... Microsoft does not provide import libraries for msvcrt4.dll anymore. ... of the compiler becomes irrelevant. ...
    (comp.lang.python)

Loading