Chapter 1
Tools and concepts

Initial version: 2025-02-19
Last update: 2025-02-19

Turning your code into executable unit, something which CPU (regardless of its architecture) may execute, consist of few steps. You don't have to know their technical details but you should know they are exist and why they are separated.

In this chapter you will learn what is a compiler and linker.

Table of contents


Compiler and compiling


A compiler is a computer program (or set of programs) that transforms source code written in a programming language (the source language) into another computer language (the target language, often having a binary form known as object code).[[sal](a href="https://fulmanski.pl/tutorial/assembler/bibliography.php#sal")] The most common reason for converting a source code is to create an executable program that is a code which can be executed by a given CPU under the control of a given operating system.

The name compiler is primarily used for programs that translate source code from a high-level programming language to a lower level language (e.g., assembly language or machine code). If the compiled program can run on a computer whose CPU or operating system is different from the one on which the compiler runs, the compiler is known as a cross-compiler. More generally, compilers are a specific type of translators.

Compilation refers to the processing of source code files (for example .c, .cc, or .cpp) and the creation of an object file. This step doesn't create anything the user can actually run. Instead, the compiler merely produces the machine language instructions that correspond to the source code file that was compiled. For instance, if you compile (but don't link) three separate files, you will have three object files created as output, each with the name .o or .obj (the extension will depend on your compiler). Each of these files contains a translation of your source code file into a machine language file -- but you can't run them yet. You need to turn them into executables your operating system can use. That's where the linker comes in.

Linker and linking


In computer science, a linker is a computer program that takes one or more object files generated by a compiler and combines them into a single executable file, library file, or another object file.

A simpler version that writes its output directly to memory is called the loader, though loading is typically considered a separate process.[sal]

Linking refers to the creation of a single executable file from multiple object files. In this step, it is common that the linker will complain about undefined functions (commonly, main itself). During compilation, if the compiler could not find the definition for a particular function, it would just assume that the function was defined in another file. If this isn't the case, there's no way the compiler would know -- it doesn't look at the contents of more than one file at a time. The linker, on the other hand, may look at multiple files and try to find references for the functions that were not mentioned.

Putting both together


Under the hood, when a program is compiled, the compiler converts the source file into object byte code. This byte code (sometimes called object code) is mnemonic instructions that only your computer architecture understands. Traditionally, these files have an .OBJ extension.

After the object file is created, the linker comes into play. More often then not, a real program that does anything useful will need to reference other files. In C, for example, a simple program to print message to the screen would consist of: printf("Hello World\n"); When the compiler compiled your program into an obj file, it simply put a reference to the printf function. The linker resolves this reference. Most programming languages have a standard library of routines to cover the basic stuff expected from that language. The linker links your OBJ file with this standard library. The linker can also link your OBJ file with other OBJ files. You can create other OBJ files that have functions that can be called by another OBJ file. The linker works, almost like a word processor's copy and paste. It "copies" out all the necessary functions your program references and creates a single executable. Sometimes other libraries that are copied out are dependent on yet other OBJ or library files. Sometimes a linker has to get pretty recursive to do its job.

Note that not all operating systems create a single executable. You can act wiser and extract and keep all common functions together in a single file. This reduces the size of your executable, but of course makes your executable dependent on these specific file.

Summary