Journey to Engineering

Search IconIcon to open search

How to Setup Ubuntu for C and C++ Development

Last updated Oct 17, 2022

In this article, I will explain how I am setting up my new Ubuntu desktop system for C/C++ development. In particular, I am going to install build-systems (Make, CMake and Ninja), compilers (GCC, Clang and Arm-GCC) and some supporting tools (e.g. Doxygen).

# Prerequisites

I learned most parts of this setup process in the course Creating a Cross-Platform Build System for Embedded Projects with CMake from Embedded Artistry. The course goes more in-depth on the installation process of other tools, and it covers all the major OSes. If you need to learn more about CMake, go for it.

# Build Systems Installation

CMake is the main tool I would like to use to create the build system. It can be installed either with apt or pip. The version installed using the first method is not updated frequently, so I decided to proceed using Python.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# Check if Python is installed
$ which python3
/usr/bin/python3

# Check Python version
$ python3 --version
Python 3.10.6

# Install pip (Python package manager)
$ sudo apt install python3-pip

# Install CMake
$ python3 -m pip install cmake

After running this last command, a warning is returned: CMake is installed in ~/.local/bin/, which is not on PATH. To run the command, I need to add the directory to the PATH variable.


If you are using Terminal setup (Zsh and Oh-My-Zsh), you need to do the following.

Instead, if you are using the default Bash shell, you need to add the following into ~/.bashrc

1
PATH="/home/<user>/.local/bin:$PATH"

Now I can check the CMake version.

1
2
$ cmake --version
cmake version 3.24.1

Finally, I can install Ninja.

1
2
3
4
5
$ sudo apt install ninja-build

# Check Ninja installation
$ ninja version
1.10.1

Make is missing because it will be installed later.

# Toolchains Installation

Now I need to install the toolchains, i.e. the compilers used to translate C into machine code.

First, let’s install the build-essential package which contains (among other things):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
$ sudo apt install build-essential

# Check Make installation
$ make --version
GNU Make 4.3
# ...more text...

# Check gcc installation
$ gcc --version
gcc (Ubuntu 11.2.0-19ubuntu1) 11.2.0
# ...more text...

I will also install the Clang compiler.

1
sudo apt install clang lld llvm clang-tools

It’s time for embedded toolchains. I would like to install the ARM GNU Toolchain. Unfortunately, this process is a little bit more complicated because ARM does not provide the toolchain on package managers.

Now, I need to add the bin directory to PATH.


You can follow the same instructions of the previous paragraph. Based on your shell, you will have something like:

1
2
3
4
5
# ZSH - Add before the `export PATH` line
path+=('/home/damiano/.local/toolchains/arm-gnu-toolchain-12.2.mpacbti-bet1-x86_64-arm-none-eabi/bin')

# BASH
PATH="/home/damiano/.local/toolchains/arm-gnu-toolchain-12.2.mpacbti-bet1-x86_64-arm-none-eabi/bin:$PATH"

Finally, I can re-open the Terminal and check if the ARM Toolchain is detected by checking its version.

1
2
3
$ arm-none-eabi-gcc --version
arm-none-eabi-gcc (Arm GNU Toolchain 12.2.MPACBTI-Bet1 (Build arm-12-mpacbti.16)) 12.2.0
# ...more text...

# Supporting Tools Installation

The core tools are now ready, but I would like to install other software generally helpful when dealing with C/C++ code.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# Install make (already installed by build-essential)
sudo apt install make

# Install pkg-config
sudo apt install pkg-config

# Install lizard (code complexity analyzer)
python3 -m pip install lizard

# Install others (documentation, formatting, etc.)
sudo apt install doxygen cppcheck gcovr lcov clang-format clang-tidy clang-tools

I will explore the usage of these tools in later posts.

# Text Editor Setup

Finally, I need a text editor. Everyone has their own favourite, and I am not here to discuss which one is the best. Personally, I find Visual Studio Code pleasant, so I installed this one.

In case you are using it too, you can find it useful to install the following extensions:

# Conclusion

If you followed this article, you should have all the tools necessary to write and build C/C++ code, including support for cross-compilation.

In the following posts, we will use them extensively.