Re: C++ Project Files?????

From: E. Robert Tisdale (
Date: 04/23/04

Date: Thu, 22 Apr 2004 16:58:22 -0700

JKop wrote:

> You have a C++ project. You have source files in it.
> When you go to compile it, the first thing that happens is
> the C preprocessor sticks the header files into each source file.

A typical C++ compiler translates your C++ program in *phases*:

1. the C preprocessor,
2. the C++ compiler proper emits assembler,
3. the assembler emits machine [object] code,
4. the link editor finds the object code and library archives,
    resolves all of the external links and emit an executable program.

These phases may themselves be executed in two or more phases.

> So now you have your ".cpp" files all ready,
> without any "#include" or "#define" in them.

No. The C preprocessor *merges* the header files with the source files
*expands* the macros and emits a single *translation unit*.

> Let's assume that there's 2 source files in this project,
> "a.cpp" and "b.cpp".
> The 2 source files are compiled separately, referring to functions
> and global variables in each other via "extern" statements, which are
> usually within header files. For instance "a.cpp" can call a function
> in "b.cpp" simply by having a function prototype.
> So both your ".cpp" files are ____________ into object files.
> (what word should I use in the blank? "compiled"?)

Compiled is fine.

> So now the [link editor] links the object files together
> and voila, now "a.cpp" and "b.cpp" share their functions,
> class definitions, global variables.
> So what we're looking at is something like this:
> .cpp & .hpp -> [translation unit] -> .s -> .o -> .exe
> Each [translation unit] has it's headers put into it

then it's turned into an assembler file

> then it's turned into an object file
> then the linker gathers up all the object files,

resolves all of the links

> and [emits] an executable file.
> ----
> What the is a library file? I've heard that once your source
> files have been turned into object files, the linker then adds
> library files, and then turns it into an executable.
> So what is a library file?

The correct term is *library archive*.
It's simply a special file containing one or more object files
and [usually] some sort of table of contents
to help the link editor find the correct object file efficiently.

> Could someone please give me a full complete list of all the
> different file types associate with a C++ project, e.g..:
> .cpp Sourcecode File
> .hpp Header File
> .o Object File

            C++ source code which should not be preprocessed.
            C++ source code which must be preprocessed.
            Note that in .cxx, the last two letters
            must both be literally x.
            Likewise, .C refers to a literal capital C.
            Assembler code.
            Assembler code which must be preprocessed.

It depends upon your implementation.

> I learned C++ from a book and I pretty much have it by the reins now,
> but except for one thing, the book had nothing in it about the actual
> form of a C++ project, i.e. how there's source files and header files
> and how the linker links object files.

That's the Integrated Development Environment (IDE)
that came with your compiler.
It is supposed to hide the details of how all of these things get done.