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.
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
and another in Example001 folder to create the executable
## Example001 cmake_minimum_required(VERSION 3.1.0) project(example) set (CMAKE_CXX_STANDARD 20) # add the Shape library add_subdirectory(Shape) # add the executable add_executable(Example Example.cpp) target_link_libraries(Example PUBLIC Shape)
I compiled Example001 on my machine with the below build.sh commands
# build.sh mkdir -p build cd build cmake .. make ./Example
It was successful. Now we can go to the next step.
Link Travis to GitHub
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
Travis builds a project if a new commit is pushed to the repo and it contains
So I added the below YAML file to the root of the project:
language: cpp dist: focal compiler: gcc os: linux sudo: true env: - BUILD_TYPE=Debug # default gcc for Ubuntu Focal is v9, upgraded to v10 here # It increases build time by ~1 min addons: apt: sources: - sourceline: “ppa:ubuntu-toolchain-r/test” packages: - gcc-10 - g++-10 install: # /usr/bin/gcc points to an older compiler on Linux. - if [ "$CXX" = "g++" ]; then export CXX="g++-10" CC="gcc-10"; fi script: - ./build.sh
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 ./build.sh 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.
Moreover, you can click on the build badge, shown in the previous picture, which gives a markdown code. It can be pasted in the readme.md of the GitHub repo to see the badge there too.
- Script to copy a directory path in Bash terminal
- What is the difference between .bashrc, .bash_profile, and .profile?
- A C++ MPI code for 2D arbitrary-thickness halo exchange with sparse blocks
- Create clone of a C++ class in polymorphic way
- How to use type erasure pattern to decouple polymorphic classes in C++?