Program Raspberry Pi 2 with C++

Operate with Raspberry Pi GPIO with Linux shell and C++ program.

Operate with GPIO via devices

Devices allows to operate with GPIO from scripts or from program. The drawback is it is required elevated privileges.

  • Start Terminal
  • Switch to root user
sudo su
  • Allocate the GPIO NN – created subfolder /sys/class/gpio/gpioNN
  • Define direction out (as output) or in (as inpout) for the GPIO
  • Write value 1 or 0 to the GPIO
ls /sys/class/gpio
echo 12 > /sys/class/gpio/export
ls /sys/class/gpio
ls /sys/class/gpio/gpio12
echo out > /sys/class/gpio/gpio12/direction
echo 1 > /sys/class/gpio/gpio12/value
echo 0 > /sys/class/gpio/gpio12/value
  • write to export number of the GPIO pin you going to use to


Release GPIO

echo 12 > /sys/class/gpio/unexport


Install Eclipse CDT with Synaptic Package Manager



Marck-eclipse-to-install-within-Synaptic-Package-Manager _006


Operate with GPIO via mmaping

Fast operations with GPIO using the ‘mmaping into /dev/mem’ approach.

Program on C++ structured as a project for multiple modules or classes.

Variant of folder structure for C++ project

c++ project folder structure

src – source files and makefile.

main.cpp – the file which contains the method main. The program is started from this method.

makefile – contains directives for util make to build files to executable and libraries files.

include – folder for header files (e.g. class1.h). Folder can contain subfolders with header files. Each cpp-file contains corresponding header-file. Keep subfolders structure same as in src folder.

obj – folder for object files (e.g. class1.o). Folder can contain subfolders with object files. Object files are generated by the compiler from cpp-files and named after them (e.g. class1.cpp is compiled to class1.o). Keep subfolders structure same as in src folder.


Create folder structure in the folder for C++ project:
for header files

mkdir include

for source files

mkdir src

for object files

mkdir src/obj

Navigate to the source files folder

cd src

Create makefile

vim makefile
Create file makefile

Keep tabs before target commands!

target: dependencies
[tab] command
CFLAGS=-c -Wall -std=c++11 -I$(IDIR)
LFLAGS=-Wall -std=c++11 -I$(IDIR)




HEADERS=$(patsubst %,$(IDIR)/%.h,$(MODULES))

OBJ=$(patsubst %,$(ODIR)/%.o,$(MODULES))

$(ODIR)/%.o: %.cpp $(HEADERS)
	$(CC) -c -o $@ $< $(CFLAGS)

	$(CC) -o ../$@ $(MAIN) $^ $(LFLAGS) $(LIBS)

	\rm *.o *~ run 

Create the main.cpp file. Name of this file can be different (it need to be defined in MAIN target of the makefile)

#include <iostream>
int main(){
	std::cout << "test" << std::endl;
	return 0;

Run the command make


If there are errors – they need to be fixed and make to be run again. If there is no error – the executable file run is created (or updated if exists) in the parent folder.

Folder structure for simple cxx project

Run the executable file run


Result of running of the simple cpp program

Operate with GPIO using the component mmapGPIO made by Hussam Al-Hertani ( Following example based on this tutorial.

Download source and header files to src and include folders. Commands below assume that the current folder is src. There is another option how to use this module shared between many projects.

wget -O mmapGpio.cpp
wget -O ../include/mmapGpio.h

Add the module name mmapGpio to MODULES variable of the makefile


Change the file main.cpp

#include <iostream>
#include <thread>
#include <chrono>
#include "mmapGpio.h"

void pauseSeconds(int seconds){

void consoleWriteLine(const std::string message){
        std::cout << message << std::endl;

int main() {

    const int PIN = 17;//use GPIO pin 17

    mmapGpio rpiGpio; // instance of mmapGpio class
    rpiGpio.setPinDir(PIN, mmapGpio::OUTPUT); // set pin as output

    // toggle pin each second
    while(1) {
        rpiGpio.writePinHigh(PIN);//set pin to high level

        rpiGpio.writePinLow(PIN);//set pin to low level
    return 0;

Run make and executable run with root permissions (required to access GPIO)

sudo ../run

Run cpp program operating GPIO with mmemGpio component

Pin GPIO17 (#11 in the Raspberry Pi connector) changes its state each second.

In case of error “bash: ../run: Permission denied” provide executable permits to the application

sudo chmod +x ../run



Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.