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.

Relevant Pages

  • Re: link error 2005 or 2001
    ... and g++ as the compiler for the remaining source files. ... linked all the object files to create the final executable file. ... twice, the part before is the decoded symbol - this is an effect of C++ ... so that file at least was compiled using a C++ compiler. ...
  • Re: C++ Project Files?????
    ... by the compiler. ... executables, because the code of the function is duplicated instead of ... object files are not really standardized. ... > unsigned long int square; ...
  • Re: ASP .NET 2.0 Unanswered questions...
    ... in both 1.1 and 2.0 there are two types of source files. ... version 1.1 of in order to support inline server script, generated source code for the page, then called the desired compiler to build a dll. ... if the aspx pages are all empty then your site was precompiled for all the aspx pages and has been batched into dll's. ... again in version 2.0 the only difference between web sites and web application is the handling of the code behind files compilation and in the case of a web applications the existence of a project file. ...
  • Re: ld(1) cannot find entry symbol _start;
    ... compile - assemble - link process on FreeBSD. ... That is adds several other object files to your own in order to get ... at which execution begins is in ... If you use 'the compiler' to manage the linking process -- e.g. ...
  • Re: SHMENUBARINFO not defined says compiler - aygshell.h defined
    ... group that supports the SDK for the desktop. ... After I add "/c" to the compiler options, i get the following and i have no ... which is used when compiling the source files. ... > Microsoft Mobile and Embedded Devices Developer Support ...