How to use travis-ci to build modern c++ using modern cmake?
Use dist: bionic
. This should meet most of the cases.
Configuring travis to use the right compiler is a bit tricky. This is how it can be done:
First of all you need to set the distribution to trusty (the newest version of ubuntu that's supported by travis-ci) and require sudo.
dist: trusty
sudo: require
Next up we set the language and the compiler:
language: cpp
compiler: gcc
So far so good. Now we can go about setting up the apt install configuration:
addons:
apt:
sources:
- ubuntu-toolchain-r-test
packages:
- gcc-6
- g++-6
- cmake
This adds the ppa for the newer version of our build tools and installs them. The next step is setting up links to the new gcc and g++. /usr/local/bin
is being searched before /usr/bin
, so that our newly installed version 6 compilers are going to be accessible with just gcc
and g++
. The beginning of your script:
should look like this:
script:
- sudo ln -s /usr/bin/gcc-6 /usr/local/bin/gcc
- sudo ln -s /usr/bin/g++-6 /usr/local/bin/g++
Add the next line as well, if you want to verify the versions of those tools:
- gcc -v && g++ -v && cmake --version
The versions that come back from these commands are as follows:
gcc: 6.2.0
g++: 6.2.0
cmake: 3.2.2
That's basically it. The complete .travis.yml looks like this:
dist: trusty
sudo: required
language:
- cpp
compiler:
- gcc
addons:
apt:
sources:
- ubuntu-toolchain-r-test
packages:
- gcc-6
- g++-6
- cmake
script:
# Link gcc-6 and g++-6 to their standard commands
- ln -s /usr/bin/gcc-6 /usr/local/bin/gcc
- ln -s /usr/bin/g++-6 /usr/local/bin/g++
# Export CC and CXX to tell cmake which compiler to use
- export CC=/usr/bin/gcc-6
- export CXX=/usr/bin/g++-6
# Check versions of gcc, g++ and cmake
- gcc -v && g++ -v && cmake --version
# Run your build commands next
I found some errors in @henne90gen's answer (or maybe they've just changed). Specifically:
- You don't need
sudo
. - You don't need to install CMake from apt. This will install an ancient CMake 2.8 from Trusty. Fortunately the build image already comes with CMake 3.9.2 (as of now).
gcc-7
doesn't get installed to/usr/local/bin
and it is already in thePATH
.
This should work:
dist: trusty
language: cpp
addons:
apt:
sources:
- ubuntu-toolchain-r-test
packages:
- gcc-7
- g++-7
script:
- export CC=gcc-7
- export CXX=g++-7
- ...
Here's a longer example that includes a modern version of Qt (with QtSVG which I'm using), and works on OSX and Linux.
os:
- linux
- osx
language: cpp
dist: trusty
addons:
apt:
sources:
- ubuntu-toolchain-r-test
- sourceline: "ppa:beineri/opt-qt-5.10.1-trusty"
packages:
- gcc-7
- g++-7
- qt510-meta-minimal
- qt510svg
- qt510imageformats
- qt510tools
before_install:
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then
brew update ;
brew install qt5 cmake ;
brew link --force qt ;
fi
script:
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then
export CC=gcc-7 ;
export CXX=g++-7 ;
source /opt/qt510/bin/qt510-env.sh ;
fi
- cmake --version
- qmake --version
- ...