Compilation and Linking process in C++
In order to be converted from human-readable source code into an executable program that a computer’s processor can comprehend, C++ programs go through a multi-step process. Compilation and linking are the main steps in this process, which frequently starts with preprocessing.
The C++ Program Development Process
The following is the standard process for creating a C++ program:
Source Code Creation: Source files text files with extensions like.cpp,.cc,.cxx,.cp, or.C—hold your C++ code. Your program’s logic and instructions reside in these folders.
Preprocessing: A preprocessor processes the source files prior to the compilation process. Directives denoted by a # symbol at the start of a line are handled in this phase. For instance, the preprocessor is told to copy the contents of the designated header file into the source code at that location by using #include . Type definitions and function prototypes, for example, are declarations required by different source files but usually not their complete definitions found in header files (e.g.,.h,.hpp extensions). A translation unit is the result of this step.
Compilation: Each translation unit, or the preprocessed source code, is subsequently converted into machine code by a compiler. Object files, which usually end in.o on UNIX systems or.obj on Windows, are where this machine code is kept.
- To make sure your code complies with C++ grammar rules, the compiler checks its syntax. If it detects errors, it will generate compiler errors.
- Lexical analysis (turning code into tokens), syntax analysis, semantic analysis (such as type checking and template instantiation), and code creation are other components of compilation.
- Different compilation is used in many C++ projects, in which different source files are compiled into their corresponding object files on their own. This is advantageous since it saves time on large projects because just the modified source file needs to be recompiled.
Linking: Compiler-generated object files are not yet executable applications. To create the final executable file, these object files are combined with any required libraries using a linker.
- Libraries are sets of pre-compiled code that offer routines for basic tasks like manipulating strings or input/output. They can be either dynamic (where information is supplied to load the shared library at runtime) or static (where the code is copied into your executable).
- Resolving references to undefined symbols (such as function calls) by locating their definitions in other object files or the designated libraries is the main responsibility of the linker. It links your program’s components and any external code it makes use of.
Code Examples for Compilation and Linking
Let’s illustrate this process with a simple “Hello, World!” program.
Source File: hello.cpp
#include <iostream> // Preprocessor directive to include the iostream header
int main() { // The main function, entry point of the program
std::cout << "Hello, world!" << std::endl; // Statement to print text to console
return 0; // Statement to indicate successful execution
}
Output:
Hello, world!
Using Visual C++ (Windows)
Compiling and Linking in one step (default behavior): The Visual C++ compiler, cl, can be used to compile and link hello.cpp directly: CL Hi there, cpp Simply put, cl.exe will automatically compile hello.cpp into an object file (such as hello.obj) by default. It will then automatically launch link.exe, which is the linker, to link hello.obj with the relevant runtime libraries to generate hello.exe. The output files are hello.exe (executable), hello.obj (object file), and hello.cpp (source).
Separate Compilation and Linking:
- Compile only: Use the /c switch to compile the source file into an object file without linking: CL/C Hi there, cpp Just the compilation step will be carried out by this command, producing hello.obj.
- Link: Link hello.obj /out:hello.exe to turn the hello.obj file into an executable. Justification: In order to take hello.obj and produce hello.exe, this specifically invokes the linker (link.exe). The output executable’s name is specified via the /out:hello.exe option.
Using GNU C++ Compiler
Using the Linux/macOS/Windows Subsystem for Linux’s GNU C++ Compiler (g++):
Compiling and Linking in one step (default behavior): Using g++, you may immediately compile and link hello.cpp: g++ Hi there, cpp Explanation: This program will create a default executable called a.out by compiling hello.cpp and automatically linking it. The output files are a.out (executable), hello.cpp (source), and hello.o (object file, which is frequently generated and then deleted by default).
Separate Compilation and Linking:
- Compile only: Without linking, the source file can be compiled into an object file: g++ -c hello.cpp Justification: By instructing g++ to only build, the -c flag creates hello.o.
- Link: The output name can be specified with -o: g++ in order to link the hello.o file into an executable. -o hello_world hello.o Justification: This creates an executable called hello_world by linking hello.o.
Running the Executable: You can launch the executable from the command line after it has been produced (for example, hello.exe on Windows or a.out/hello_world on Linux/macOS):
- Hello.exe on Windows
- Use./a.out or./hello_world on Linux or macOS.
Next, the application will show: Hi there, world!
Importance of the Process
There are several benefits to using this multi-step strategy for software development.
- Modularity: Code organisation and reusability can be improved by dividing programs into smaller, more manageable source files that can be independently built.
- Faster Development Cycles: Build times for large projects are significantly reduced because only updated source files need to be recompiled because of independent compilation.
- Code Reusability: Instead of having to “invent the wheel” for typical features, libraries enable developers to reuse previously developed and tested code.
- Abstraction and Encapsulation: By enabling implementation details to be concealed within compiled libraries and only exposing essential interfaces through header files, the procedure adheres to C++’s object-oriented concepts.
You can also read High Level vs Middle Level vs Low Level Languages Explained