Compiling C++ Programs

Why Compile?

Recall:

A translation of the C++ program statements to machine language is necessary.

Stages in Compiling

A number of steps are involved in the compilation process.

                                                 other ML   ----+
                                                                V       
         +--------------+   +----------+   +-----------+   +--------+
  C++ -->| PREPROCESSOR |-->| COMPILER |-->| ASSEMBLER |-->| LINKER |-->  
Program  +--------------+   +----------+   +-----------+   +--------+     
 
  Output:  Modified C++       Assembly      Executable     "Complete"
           program (copy)     language        code (ML)    Executable

Stages in Compiling - Explanation

PREPROCESSOR

The source program undergoes modification before it is compiled. A modified copy of the original program is created, however it is still valid C++. Things the preprocessor does includes:

  • #includes are processed. In the copy of the original file, the #include is removed, and C++ statements are inserted at the point of the #include just as if the programmer had typed them in themself.

  • Comments are removed.

COMPILER

The C++ statements are converted into AL statements.

ASSEMBLER

The AL statements are converted into ML called object code.

LINKER

Object code from multiple sources is combined together to form a complete executable program. For example the ML to perform input/output is almost always linked in since most programs do input or output.

Linking

Linking is an important phase of the compilation process.

All machine language can be compiled into a single executable program. This is called static linking. More common today is dynamic linking. With dynamic linking the "other" machine language remains separate and the linker arranges for your program to connect to the "other" machine language. Typing, at the prompt:

ldd executable_file_name

will show the dynamic libraries (files) the program requires. Try a.out for the executable_file_name.

When the program name is typed at the prompt the command interpreter, usually called the shell, causes the operating system to load the program into memory and begin executing it.

Note that the whole process is also called compiling and gets its name from the subpart, the conversion from C++ to AL.

clang++ Compiling Options

clang++ file.cpp

Compile file file.cpp and create an executable named a.out

clang++ file.cpp -o file2

Compile file file.cpp, create an executable, and name the executable file2 instead of a.out

clang++ -Wall file.cpp

Output warnings
Always compile with the warnings option

clang++ -c file.cpp

Run the first 3 stages, but not the linker.
The machine language is put in file.o. This will be used when a program consists of more than one file, the linker will then be run on all the .o files.

clang++ -E file.cpp

Run the first stage of the compiler, the preprocessor, only.
Output is sent to stdout (standard output, by default the terminal window).

clang++ -H file.cpp

Show header file inclusion only.
Header files may include other header files. Including only iostream causes many other files to be included.

clang++ --version

Output the version of clang++


URL: https://data-structures.cs.kent.edu/labs/Info/compiling.html
Last update: EST