Compile Your Own C/C++ Programs with GCC on Raspberry Pi

A step-by-step guide to installing the GNU Compiler Collection (GCC) on your Raspberry Pi for compiling and running C/C++ programs. …


Updated August 2, 2023

Need help with your Raspberry Pi?
Contact Me!

Do you love silly Raspberry Pi Projects?
Check out my this YouTube Channel!


A step-by-step guide to installing the GNU Compiler Collection (GCC) on your Raspberry Pi for compiling and running C/C++ programs.

Raspberry Pi is a popular single-board computer used for various purposes, from home automation to AI development. One of the most common uses of Raspberry Pi is to run scripts written in programming languages like Python or C/C++. To run C/C++ programs on Raspberry Pi, you need to have a compiler installed that can convert your source code into machine code that the computer can understand and execute. GCC (GNU Compiler Collection) is one of the most popular compilers used for this purpose. In this article, we will walk through the steps to install GCC on Raspberry Pi.

Before you begin, make sure your Raspberry Pi has a working internet connection. You can connect it to your router using an Ethernet cable or set up Wi-Fi by following the instructions in the official documentation: https://www.raspberrypi.org/documentation/configuration/wireless/wireless-cli.md

  1. Update and Upgrade Your Raspberry Pi

Open a terminal window on your Raspberry Pi (you can do this by pressing Ctrl+Alt+T) and run the following commands:

sudo apt update
sudo apt upgrade -y

This will ensure that your system is up-to-date with the latest packages and dependencies.

  1. Install GCC

Now, we can install GCC using the following command:

sudo apt install build-essential -y

This command installs a collection of packages that includes GCC, make (a tool for building programs), and other essential tools required to compile and run C/C++ programs.

  1. Verify Your Installation

To verify that the installation was successful, you can check the version of GCC by running:

gcc --version

You should see output similar to this:

gcc (Raspbian 8.3.0-6+rpi1) 8.3.0
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  1. Compile and Run a Sample Program

To test that GCC is working, let’s compile and run a simple “Hello World” program in C++. Create a new file called hello.cpp using your favorite text editor (e.g., nano):

nano hello.cpp

Paste the following code into the file:

#include <iostream>

int main() {
  std::cout << "Hello World!" << std::endl;
  return 0;
}

Save and exit the editor (in nano, press Ctrl+X, then Y to save and Enter to confirm the filename). Now compile the program using GCC:

g++ hello.cpp -o hello

This command compiles your code into an executable called hello. To run the program, simply type:

./hello

You should see the output:

Hello World!

And that’s it! You have successfully installed GCC on your Raspberry Pi and can now compile and run C/C++ programs. With this tool in hand, you can start exploring more complex projects and leverage the power of Raspberry Pi for a wide range of applications.