How to compile a binary, from code to process
Published on 2026-09-12 by frfrand
The four stages that turn code into a program, what the linker does and why it fails, static or dynamic linking, and what happens when it runs.

Short answer: a compiled program goes through four stages: preprocessing (pastes files and expands macros), compilation (generates an object file for each source), linking (resolves pending names and joins libraries), and loading (the kernel and dynamic loader put it into memory). Knowing which stage you are in tells you what each error means.
You write code, run a command, an executable appears. In between there are four stages, and knowing which is which completely changes the interpretation of errors.
1. Preprocessing
Directives are resolved: included files are pasted, macros are expanded, conditional blocks that do not apply are removed.
The result is still text, normally much longer than the original.
2. Compilation
The compiler translates that text into machine instructions. It verifies syntax, applies optimizations, and generates an object file for each source file.
An object file has machine code, but it is not executable yet: it contains references to things that are elsewhere. If your code calls a function defined in another file, the object saves the name and a space to fill in.
Errors at this stage are the familiar ones: a semicolon, a type that does not match, an undeclared variable. They tell you file and line.
3. Linking
This is where the most confusing errors appear.
The linker joins all objects and libraries, and resolves each pending reference: for each name used, it looks up where it is defined and notes the address.
If a name does not appear anywhere, it fails with the classic:
undefined reference to 'mi_funcion'
This error is confusing because it looks like a compilation error and it is not. The compiler was perfectly happy: it knew that function existed somewhere. The one that cannot find it is the linker.
Common causes: forgetting to add a file to the compilation, forgetting to link a library (the classic -lm for math), or the library order is wrong (the linker processes them in order and some cases are sensitive to that).
Static or dynamic
A decision with very practical consequences.
Static linking: the library code is copied inside your executable. The binary is larger and does not depend on external libraries: you copy it to another machine with the same system and architecture, and it works.
Dynamic linking: the executable only stores the library name, and it is loaded at execution time. The binary is small, and several programs share the same library in memory. Also, a security update of the library benefits all programs without recompiling.
The cost of dynamic linking is the error at the other extreme:
error while loading shared libraries: libfoo.so.3: cannot open shared object file
That is runtime: the program compiled and linked fine, but when starting it does not find the library. To see what a binary depends on:
ldd /usr/bin/miprograma
This is why a binary compiled on one distribution may not run on another, and it is also one of the reasons containers exist: to encapsulate the exact set of libraries.
4. Load and execute
When you run the program, the kernel reads the file header (in Linux, ELF format), reserves memory, maps the code and data sections, and passes control to the dynamic loader, which resolves the missing libraries and finally jumps to the entry point.
Curiosity: the code segment is mapped as read-only and shared. If you run the same program twenty times, the code is only once in physical memory.
And languages that do not compile like this
Python, JavaScript, or Ruby do not produce a native binary. They are interpreted, or compiled to an intermediate format that runs on a virtual machine.
Java and C# are in the middle: they compile to bytecode and translate it to native code while running.
Go and Rust go the other way, and that's why they became popular in infrastructure: they produce a native binary that carries almost everything it uses inside. Go, if it doesn't use cgo, generates a static binary by default. Rust links its own dependencies into the binary, but by default it still uses the system's C library (glibc); for a fully static one, it's compiled against musl. In both cases, you copy the file to a server of the same architecture and it runs, without an interpreter or virtual environment.
From compiled code to the process that serves a website
After compiling an application, you still need to decide how to start it, what variables it receives, from which directory it runs, and how it restarts if it fails. A correct binary can fail in production if it lacks libraries, permissions, or paths.
On an ArduMaker VPS you can run compiled applications as processes and manage them through services. A common flow is to build the project, run tests, publish the necessary files, configure systemd, and put Nginx in front if it is an HTTP API. Keep the logs to distinguish a binary problem from a deployment problem.
Related: What is a process · From Git to production.
Frequently asked questions
"undefined reference" is a compilation error? No. It's from the linker: the compiler accepted the code, but when linking the objects no one defined that name. Almost always a file is missing, a library is not linked (-lm, for example) or they are in the wrong order.
What does "error while loading shared libraries" mean? That the program compiled and linked correctly, but when starting it cannot find a dynamic library. ldd shows which libraries it depends on and which ones are missing.
Does a static binary run on any Linux? On any of the same architecture (x86_64, ARM64…) and with a compatible kernel, yes, because it does not depend on installed libraries. It does not run on another architecture or another operating system.