Building a C++ project with Travis continuous integration (CI)


I explain with a simple example that how a C++ project is automatically built by Travis CI with the push of a commit. The operating system (OS) is Ubuntu and the compiler is GCC.

Why Travis

Because it has detailed documentation and is free for open-source projects.


The code is placed here. Here, the main focus is on Example001.

Build system (CMake)

Before configuring continuous integration (CI), ensure the project has a build system generator like CMake and it is successfully compiled and run on a local machine.

For this example (see the code), I use CMake. We have a CMakeLists.txt in the Example001/Shape as

add_library(Shape Box.cpp)

and another in Example001 folder to create the executable

## Example001

cmake_minimum_required(VERSION 3.1.0)

# add the Shape library

# add the executable
add_executable(Example Example.cpp)

target_link_libraries(Example PUBLIC Shape)

I compiled Example001 on my machine with the below commands

mkdir -p build
cd build
cmake ..

It was successful. Now we can go to the next step.

Login to the Travis CI website with aid of your GitHub account. Click on your profile picture in Travis then settings. Find the desired project and activate continuous integration (CI) as shown below

(.Get 1)

YAML file

Travis builds a project if a new commit is pushed to the repo and it contains .travis.yml file.

So I added the below YAML file to the root of the project:

language: cpp 
dist: focal
compiler: gcc
os: linux
sudo: true
    - BUILD_TYPE=Debug

# default gcc for Ubuntu Focal is v9, upgraded to v10 here
# It increases build time by ~1 min
                  - sourceline: “ppa:ubuntu-toolchain-r/test”
                  - gcc-10
                  - g++-10
# /usr/bin/gcc points to an older compiler on Linux.
- if [ "$CXX" = "g++" ]; then export CXX="g++-10" CC="gcc-10"; fi
  - ./

The first line is to set C++ as the code language.

The second line is the distribution of Ubuntu. Focal is Ubuntu 20.04 which comes with GCC v9. You can choose Bionic 18.04, Xenial 16.04 and so on.

The OS is Linux.

sudo is true so we can install new packages.

env line shows how we can set an environment variable.

addons are the packages to be installed before compilation. Here, I added GCC v10. Note installing GCC took about 25 seconds but the example itself compiled in 2 seconds. If not necessary, stick to default packages that come with the distribution.

The last line ./ runs CMake commands.

Push a commit

Finally, go to your code, make a change, commit and push it to GitHub. Travis should automatically build it for you. You can see the live results in the focused project on the Travis website.

(.Get 1)

Build badge

Moreover, you can click on the build badge, shown in the previous picture, which gives a markdown code. It can be pasted in the of the GitHub repo to see the badge there too.

(.Get 1)


Travis docs

Tags ➡ C++


I notify you of my new posts

Latest Posts


0 comment