Re: How do header files work?
From: Howard (alicebt_at_hotmail.com)
Date: 05/19/04
- Next message: Joona I Palaste: "Re: unhandled exception : Privileged instruction"
- Previous message: Niels Dekker (no reply address): "Usual arithmetic conversions + integral promotion for short?"
- In reply to: matthurne: "Re: How do header files work?"
- Next in thread: dpr: "Re: How do header files work?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Joona I Palaste: "Re: unhandled exception : Privileged instruction"
- Previous message: Niels Dekker (no reply address): "Usual arithmetic conversions + integral promotion for short?"
- In reply to: matthurne: "Re: How do header files work?"
- Next in thread: dpr: "Re: How do header files work?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|