Ch1.3: Compilers
Why Do We Need a Compiler?
A compiler is the tool that transforms human‑readable C++ source code into machine instructions. Without a compiler, our programs would remain text files and could never be executed by the CPU.
Compilers do much more than just translation. They also link your program with system libraries,
dynamic link libraries (DLLs), and other parts of the operating system. This allows your program to call functions
like malloc, printf, or system APIs.
Beyond linking, the compiler and toolchain make assumptions about the platform itself: how system calls work, how memory is laid out, how threads and processes are managed, and which Application Binary Interface (ABI) conventions are followed. These assumptions are baked into the generated binary.
That is why binaries are not portable across operating systems even if they share the same format. For example, a GNU/Linux binary will not run on FreeBSD, musl‑based systems, or Android, despite all of them using ELF binaries. The difference lies in the libraries, ABIs, and platform conventions each system provides.
Installation
You can install these compilers directly by visiting their release pages:
- GCC Releases
- Clang/LLVM Releases
- Windows MSVC Sysroot (required if using Clang to compile Windows programs)
These compilers are also built and maintained by the author of the fast_io library, ensuring they are tailored to work seamlessly with the examples in this tutorial.
In the next chapters, we will explain how to set up Clang first in Ch1.4 and GCC in Ch1.5. You can pick either compiler to use — both are supported.
Set Environment Variables
After downloading, you should set environment variables so your shell can locate the compiler binaries:
- PATH: add the compiler’s
bindirectory so commands likegccorclangcan be found. - LD_LIBRARY_PATH: (Linux/BSD/Android, etc) add the compiler’s library directory so dynamic libraries can be resolved.
- DYLD_LIBRARY_PATH: (macOS) add the compiler’s library directory for dynamic linking.
- Windows: only
PATHis needed; noLD_LIBRARY_PATH.
Setting these variables ensures that your terminal can find both the compiler executables and the libraries they depend on.