6 min read

Bare Metal Foundations 00 - Primer & Setup

Pixel art of a programmer sitting in front of three monito

Bare Metal Foundations is a crash course to computer programming, designed for people who have little or no programming experience.

The purpose of Bare Metal Foundations is to enable new programmers to get up to scratch quickly in order to access Bare Metal Fundamentals, which is our upcoming comprehensive course designed to teach systems programming and computer fundamentals.

Many courses start with 'simpler' programming languages and tools in order to give readers the impression that they're getting things done fast. We're not going to insult your intelligence: we're going to be writing portable code in C and running on the terminal from day 1.

🖥️
The terminal, also called the command line, is a text-based interface to your computer.

It might feel strange at first, especially if you're coming from a Windows background, but there are only a few commands that you really need to learn and then it becomes second nature.

Why? Because taking shortcuts denies you the opportunity to understand what your code is really doing, which results in a knowledge gap which makes it impossible for you to reason about the computer.

Goals For This Post

In order to get started, we're going to need to install some tools:

  • Clang/LLVM - This is the compiler. It takes human-readable source code and turns it into machine-readable programs so that our computers can run it.
  • Git - This is a version control system. It allows us to record the history of our code, and also to fetch code from the Internet.
  • An Editor - I'll provide instructions to install & use VS Code, a very common cross-platform editor by Microsoft. If you're comfortable with a different editor, keep using that, just make sure you have C syntax highlighting.

We're going to provide instructions for Windows, Mac, and Linux.

We'll also verify that everything installed correctly, and learn a few basic terminal commands that every programmer should know.

Windows Setup Instructions

Don't worry if this section looks a little intimidating, it takes about five minutes and you'll only have to do it once.

⚛️
By default, developing on Windows is quite different to developing on Linux & Mac. This is because Windows doesn't follow POSIX standards (if you don't know what that means, don't worry about it for now).

In order to be able to write the same code everywhere, we're going to use a set of tools called MSYS2 which makes Windows act more like everything else.

Installing MSYS2

  1. Download the installer from the MSYS2 website.
  2. Run the installer, keep all the defaults.
  3. MSYS2 will open a terminal window.

Note: You can always get back to the MSYS2 terminal by opening the MSYS2 UCRT64 shortcut from the start menu. You can ignore the rest of the MSYS2 shortcuts for the purpose of this series.

Installing clang, llvm & git

Once you have the terminal open, update the available packages:

pacman -Syu

If this closes the terminal, reopen it by going to the Start menu and clicking on the MSYS2 UCRT64 shortcut.

Now, install the packages you'll need by typing these commands into the terminal:

pacman -S git
pacman -S mingw-w64-ucrt-x86_64-clang
pacman -S mingw-w64-ucrt-x86_64-lldb

That's it, now you can move on to 'Verifying The Installation'.

Linux Setup Instructions

⚠️
There are as many Linux distributions as there are hours in the year, and it would be impossible to provide comprehensive instructions for all of them.

These instructions are written for Ubuntu 24.04 (or any Debian-based distro like Pop!_OS, Mint, etc.).

If you're on another distribution, search your package manager for clang, llvm, lldb, and git (it's very likely that those will be the package names) and install them.

Installing via the terminal on Linux is very simple, just run:

sudo apt update
sudo apt install clang llvm lldb git

If you're on an older distribution...

If your Linux distribution is very out of date, it might provide really old versions of clang/llvm which won't support the latest C23 standard.

If this is the case, you can follow the Mac instructions below to install clang/llvm/lldb with Homebrew, but you'll have to make sure you don't have them installed through your package manager. Alternatively, consider updating your distribution, it probably has serious security issues.

Mac Setup Instructions

Installing Homebrew

On Mac, the easiest way to install these tools is via Homebrew.

  1. Press cmd-space and type 'Terminal', then hit enter.
  2. Install Homebrew with the command from the Homebrew website.

Installing Clang, LLVM & git

Once Homebrew is installed, run this command in the terminal:

brew install llvm git

Updating Your PATH

💡
PATH is a standard 'environment variable'. It tells your operating system where to look for executable programs when you write them on the command line.

On MacOS, if you have XCode installed, it'll come with an old version of clang, lldb & llvm. In order to use the newest one, run:

echo 'export PATH="/opt/homebrew/opt/llvm/bin:$PATH"' >> ~/.zshrc
source ~/.zshrc

The first line will update your system PATH to look for programs in /opt/homebrew/opt/llvm/bin before it looks anywhere else, which will mean you'll use the newer versions of the LLVM tools, not the ones from XCode.

The second line tells your current terminal session to reload the settings (in this case, to update the PATH.

Verifying The Installation

In order to verify your installation, run the following commands:

clang --version

You're looking for a version of at least 18.0.0 here. This is the earliest version of clang which supports most of C23.

lldb --version

As long as you get some output here, and not command not found, you're good.

git --version

Similar to the above, as long as you get some useful output here, you're good.

Installing VS Code

If you're already comfortable with a different text editor (Visual Studio, CLion, vim, emacs, Sublime, whatever floats your boat), you can skip this section. Just make sure you have C syntax highlighting support.

You can download VS Code from Microsoft's website.

On Windows and Linux, this will automatically add code to your PATH, meaning that you can type it on the command line and open VS Code.

Add To Path (Mac Only)

On Mac, you'll need to run this on your Terminal to get code on your PATH:

cat << EOF >> ~/.zprofile
# Add Visual Studio Code (code)
export PATH="\$PATH:/Applications/Visual Studio Code.app/Contents/Resources/app/bin"
EOF

Then close your terminal and open a new one (cmd-space, type terminal).

Make Sure VS Code Works

Open it up to make sure it works. Just run this on your terminal (the . just means 'open it in the current directory'):

code .

Phew, now we have everything installed.

Terminal Fly-By

So that we're ready to hit the ground running with code in the next chapter, we're going to learn a few terminal commands and set up a workspace.

Some Common Commands

Here's three commands that you'll be using a lot:

  • pwd is short for 'present working directory' (it has nothing to do with passwords). When you type it, the terminal will tell you where you're working right now.
  • cd <directory> means 'change directory'. It moves your present working directory to wherever you tell it to.
    • cd ~ is shorthand for 'change directory to home'. The home directory is your base of operations, and usually contains all of your documents etc.
  • mkdir <directory> means 'make directory'. It makes a new directory wherever you tell it to.
    • mkdir -p is shorthand for 'make directory with parents'. Instead of mkdir code then mkdir code/my-app, you can just do mkdir -p code/my-app.

About Directories

Directories (you may know them as 'folders') are how files are organised on your computer. If you've ever had to trawl through My Documents then Reports etc, you know how this works, it's just expressed a bit differently on terminal.

🏡
~ or 'home' can be thought of as a directory that just belongs to your user, it's a convention from when lots of users used to share a mainframe computer, and each would have their own 'home'.

On Windows (with MSYS2), home is at C:\msys64\home\<user>.
On Linux, you can find it at /home/<user>.
On Mac, it's at /Users/<user>.

Quick note: almost all systems choose to separate directories with a / (~/Documents/Reports), but Windows uses \ instead.

With MSYS2, you can use either of the two.

Setting up a Workspace

Let's quickly set up a workspace. Run these commands on your terminal:

cd ~
mkdir -p code/bare-metal/foundations

Now you can get to your foundations directory at any time by running:

cd ~/code/bare-metal/foundations

And we're there!

Thanks for bearing with me through setup, it's not the most intuitive thing in the world but you only have to do it once (until you get a new computer, at least).

Next, we'll go through how to write, compile, and run multiple simple C programs.