Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 440
henne90gen Avatar asked Jan 29 '17 01:01

henne90gen


4 Answers

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
like image 169
henne90gen Avatar answered Nov 12 '22 00:11

henne90gen


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 the PATH.

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
  - ...
like image 22
Timmmm Avatar answered Nov 11 '22 23:11

Timmmm


Use dist: bionic. This should meet most of the cases.

like image 3
imba-tjd Avatar answered Nov 11 '22 23:11

imba-tjd


Adding a semi-related solution after struggling with this for way too long. Hopefully, it helps someone else avoid spending the amount of time I did going through the cycle of updating travis.yml, committing, waiting for Travis...repeat til it works.

I had a C extension in a Python project that started producing failing tests in Travis, yet passed locally. Eventually, I tracked it down to the old version of gcc on the xenial environment. Here's the Travis YAML file that finally solved the issue for me:

dist: xenial
language: python
before_install:
  - sudo add-apt-repository ppa:ubuntu-toolchain-r/test -y
  - sudo apt-get update -q
  - sudo apt-get install -y gcc-7
  - export CC=/usr/bin/gcc-7
python:
  - "3.6"
  - "3.7"
install:
  - pip install -r requirements.txt
  - python setup.py build_ext --inplace
script:
  - python run_tests.py

As an aside, anyone else find they want to trigger a Travis build on a specific commit to find out exactly where a problem started?

like image 2
Fiver Avatar answered Nov 11 '22 23:11

Fiver