C++ Compilation Process (Preprocessor, Assembling & Linking)

CWC
7 Min Read

Do you know what happens when you hit that compile button in your editor? No, not the little red squiggly lines – I’m talking about what actually happens behind the scenes to take your C++ code and turn it into a program that your computer can run. It’s a bit more complicated than just typing in some code and hitting run! In this blog post, we’ll take a look at the compilation process for C++ programs, from preprocessing to assembling to linking.

C ++ Programming: C++ Compilation Process (Preprocessor, Assembling & Linking)

The C++ compilation process is a two-step process: first, the source code is compiled into an intermediate representation called object code; then, the object code is linked with other object files and libraries to create an executable file.

The compiler translates the source code into machine code, which can be run on a computer.

  1. The C++ preprocessor combines the content of any included header files, generates macro code, and substitutes values of symbolic constants defined with #outline with their original values in the source code record. As part of the compilation process, the C++ preprocessor inserts the contents of any header files that have been included into the source code record. This allows the compiler to access all of the necessary code in a single file. In addition, the preprocessor creates macro code. This allows for symbolic constants to be defined with #define and substituted.
  2. The C++ preprocessor converts the improved source code file created by it into a platform-appropriate meeting language.
  3. The compiler creates assembler code and, when compiled, converts it to platform-appropriate object code.
  4. The output file produced by the assembler is linked with the object code files for any library Functions required to generate an executable file.
  5. The compiled code does not make up the complete program. A small amount of compiled code is linked together, but it does not form the final application. This “related” code, which may also be included in a layout that can be utilized by other packages, is referred to as a library by C++ developers.

For convenience or additional analysis, many C++ compilers may combine or split certain parts of the compilation process. Many C++ coders use specialized utilities, but when developing a program, they will generally follow this basic procedure.

C++ Compilation Process – The Preprocessor

The preprocessor is a piece of code that helps you include other files in your project without worrying about the syntax C+ + translation. It works on one source file at time by replacing #include directives with their contents ( Usually just declaration), doing replacement for macros define statements ifdef/endif conditions and selections dependent upon an instruction called “#if

The preprocessor produces a single output, a stream of tokens resulted from the modifications described above, at the end of all this. It also includes some unique markers that tell the compiler where each line was produced so that it can provide helpful error messages. With clever use of #if and #error directives, certain problems may be manifested.

C++ Compilation Process – Compilation

The preprocessor’s output is compiled in the assembly stage. The compiler converts pure C++ source code into assembly language, which it then parses and compiles.

The assembler in the toolchain then invokes the underlying back-end (assembler in the toolchain) which compiles that code into machine language and produces a real binary file in some format (ELF, COFF, a.out, etc.)

This file contains the syntax of the symbols defined in the input. Symbols are referred to by name in object files.

Symbols that aren’t defined can be referred to in object files. When you use a declaration without providing a definition for it, this is the case.

This is a good example of when the cast was necessary, since we’re defining an array inside another. The C# compiler isn’t concerned by this, and will happily generate the object file as long as the source code is valid.

Compilers usually allow you to stop compilation at this stage. This is very beneficial because it allows you to compile each source code file separately. The benefit of this is that you don’t have to recompile everything if only one file is updated.

C++ Compilation Process – Assembling

The assembler converts source code to object code. You may see files with a .o extension on a UNIX system that indicate object code files (.OBJ on MSDOS)

The assembler goes through the object files, converting them from assembly code to machine language instructions. The file produced is a relocatable object code.

As a result, the compilation process generates a relocatable object program that can be used in different locations without requiring further compilation.

C++ Compilation Process – Linking

The linker is the program that turns the object files generated by the assembler into a complete compilation output. This final output might be a shared (or dynamic) library (which is named similarly to static libraries), or it can be an executable.

It resolves symbols that haven’t been defined in any file by replacing the references to missing symbols with the correct addresses. These symbols can be defined in additional object files or libraries. If they’re defined in libraries other than the main one, you’ll need to tell the linker about them.

The most common errors at this point are missing definitions or duplicate definitions. The former implies that the terms do not exist (i.e., they have not been written), whereas the latter implies that the same symbol was defined twice in two separate object files or libraries.

Share This Article
Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *

English
Exit mobile version