Re: How do header files work?

From: Howard (alicebt_at_hotmail.com)
Date: 05/19/04


Date: Wed, 19 May 2004 19:41:08 GMT


"matthurne" <matthew_hurne@yahoo.com> wrote in message
news:4bdff256.0405191004.5012e5b4@posting.google.com...
> Jeff Schwab <jeffplus@comcast.net> wrote in message
news:<jsOdnSeU0-h46zbdRVn-hw@comcast.com>...
> > matthurne wrote:
> > > I just started learning C++ on my own...I'm using Accelerated C++.
> > > Something it hasn't explained and I keep wondering about is how header
> > > files actually work. I suspect it doesn't get into it because it is,
> > > as the authors love to say, "implementation specific". If that's the
> > > case, how does the compiler commonly handle them? I use Linux and gcc
> > > 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
> > > understanding I could name my header files something completely
> > > unrelated to the source files which contain the definitions and
> > > including the header files would still make the defined
> > > functions/whatnot usable. How?
> >
> > The #include directive just tells the preprocessor to go read a
> > different file, just as though the contents of that file had been
> > included inline. There's no other magic there. You can #include any
> > file you like, regardless of what its name is.
>
> Ok, fair enough. That is helpful...however, header files will
> generally only contain function declarations, not definitions,
> correct? If this is the case, how does including just the declaration
> allow the function to actually work when there is no #include of the
> source file which contains the function's full-blown definition?

The compiler has to know which source files it's going to compile in the
first place, right? Usually, that means listing them in some kind of
project file. Those are the files that contain the implementation code.
You don't compile the headers, you compile the source files, and those
include the headers they specify.

In some cases, you don't have the source file, but only a header file and
some kind of library. In that case, your project has to specify the library
where the compiled sources reside.

So, you compile all the sources based on the list of source files (or
whatever), and those get the headers included which they specify, then these
compilation units are compiled into object code, and afterwards everything
gets linked up (as needed, depending upon what it is you're building).

-Howard



Relevant Pages

  • Re: How to scan C files to find the required include files ?
    ... >files for c source files. ... Comment out all headers that are internal in the botom source. ... Compile the files at the bottom of the internal dependency graph. ... etc)/Perl/Python/Ruby script to do this. ...
    (comp.unix.programmer)
  • Re: Compile java sources inside J2EE component
    ... >>After creating the source files I need ... >>to compile them at runtime, is there a possibility to do so? ... to define his specific CRM model at runtime. ... java classes that represent this model and instances of this model to ...
    (comp.lang.java.programmer)
  • Re: Utility to ensure appropriate headers were included
    ... failed to compile. ... the problem was that one of the source files was missing: ... Presumably you mean that one of the system headers included stdio.h, ... because all the identifiers they declare are ...
    (comp.lang.c)
  • Re: Multilangual : how to?
    ... texts into separate *.rc source files John. ... maintenance point of view to keep them in the same source file, but compile ... Also, error-message texts really belong in a separate, more specialised *.mc ... source files get compiled by the mc message compiler. ...
    (microsoft.public.vb.general.discussion)
  • Re: Linking options
    ... Link all source files to one VS.NET project (literally add them into ... VS.NET) then compile those modules into your final assembly. ... > Is it possible to do a static link when building an executable? ... > little application that uses 3 or 4 of my custom DLLs. ...
    (microsoft.public.dotnet.languages.csharp)