How to use travis-ci to build modern c++ using modern cmake?

henne90gen picture henne90gen · Jan 29, 2017 · Viewed 8.2k times · Source

Is it possible to use travis-ci to build a c++ application/project that uses cmake, gcc-6 and g++-6?

Answer

henne90gen picture henne90gen · Jan 29, 2017

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