Building a C Project

What This Guide Covers #

Language versions and other build-environment specific information are in our reference pages:

This guide covers build environment and configuration topics specific to C projects. Please make sure to read our Tutorial and general build configuration guides first.

CI environment for C Projects #

Travis CI VMs are 64-bit and provide versions of:

  • gcc
  • clang
  • core GNU build toolchain (autotools, make), cmake, scons

C projects on Travis CI assume you use Autotools and Make by default.

For precise versions on the VM, please consult “Build system information” in the build log.

Dependency Management #

Because there is no dominant convention in the community about dependency management, Travis CI skips dependency installation for C projects.

If you need to install dependencies before your tests can run, override the install: key in your .travis.yml:

install: make get-deps

See the build configuration guide to learn more.

Default Build Script #

The default build command is:

./configure && make && make test

Projects that find this sufficient can use a very minimalistic .travis.yml file:

language: c

You can change the build script as described in the build configuration guide:

script: scons

Choosing compilers to test against #

You can test projects against either GCC or Clang, or both. To do so, specify the compiler to use using the compiler: key in .travis.yml. For example, to build with Clang:

compiler: clang

or both GCC and Clang:

compiler:
  - clang
  - gcc

Testing against two compilers will create (at least) 2 rows in your build matrix. For each row, Travis CI C builder will export the CC and CC_FOR_BUILD env variables to point to either gcc or clang.

On OS X, gcc is an alias for clang. Set a specific GCC version to use GCC on OS X.

Build Matrix #

For C projects, env and compiler can be given as arrays to construct a build matrix.

OpenMP projects #

OpenMP projects should set the environment variable OMP_NUM_THREADS to a reasonably small value (say, 4). OpenMP detects the cores on the hosting hardware, rather than the VM on which your tests run.

MPI projects #

The default environment variable $CC is known to interfere with MPI projects. In this case, we recommend unsetting it:

before_install:
  - test -n $CC && unset CC

C11/C++11 (and Beyond) and Toolchain Versioning #

If your project requires tools compatible with C11, C++11, or a more recent language standard, then it is likely that you will have to upgrade your compiler and/or build tools. This section covers specifically how to upgrade GCC, clang, and cmake; for other dependencies please see Installing Dependencies.

GCC on Linux #

Ubuntu 12.04 ships with GCC 4.6.3 and Ubuntu 14.04 ships with GCC 4.8.2.

Note that GCC support for ISO C11 reached a similar level of completeness as ISO C99 in 4.9 and that C++11 is feature-complete in 4.8.1, but support for <regex> does not exist until 4.9.

To upgrade GCC to a more recent version, you will have to install the appropriate version from the ubuntu-toolchain-r-test source; see below for examples:

matrix:
  include:
    # works on Precise and Trusty
    - os: linux
      addons:
        apt:
          sources:
            - ubuntu-toolchain-r-test
          packages:
            - g++-4.9
      env:
         - MATRIX_EVAL="CC=gcc-4.9 && CXX=g++-4.9"

    # works on Precise and Trusty
    - os: linux
      addons:
        apt:
          sources:
            - ubuntu-toolchain-r-test
          packages:
            - g++-5
      env:
         - MATRIX_EVAL="CC=gcc-5 && CXX=g++-5"

    # works on Precise and Trusty
    - os: linux
      addons:
        apt:
          sources:
            - ubuntu-toolchain-r-test
          packages:
            - g++-6
      env:
        - MATRIX_EVAL="CC=gcc-6 && CXX=g++-6"

    # works on Precise and Trusty
    - os: linux
      addons:
        apt:
          sources:
            - ubuntu-toolchain-r-test
          packages:
            - g++-7
      env:
        - MATRIX_EVAL="CC=gcc-7 && CXX=g++-7"

before_install:
    - eval "${MATRIX_EVAL}"

GCC on OS X #

On OS X, gcc is an alias for clang, and g++ is an alias for clang++. So you must set CC and CXX to specific gcc/g++ versions:

matrix:
  include:
    - os: osx
      osx_image: xcode8
      env:
        - MATRIX_EVAL="CC=gcc-4.9 && CXX=g++-4.9"

    - os: osx
      osx_image: xcode8
      env:
        - MATRIX_EVAL="brew install gcc5 && CC=gcc-5 && CXX=g++-5"

    - os: osx
      osx_image: xcode8
      env:
        - MATRIX_EVAL="brew install gcc6 && CC=gcc-6 && CXX=g++-6"

    - os: osx
      osx_image: xcode8
      env:
        - MATRIX_EVAL="brew install gcc && CC=gcc-7 && CXX=g++-7"

before_install:
    - eval "${MATRIX_EVAL}"

Clang #

Ubuntu 12.04 ships with Clang 3.4 and Ubuntu 14.04 ships with Clang 3.5.0.

Note that C++11 support is complete starting from Clang 3.3.

To upgrade Clang to a more recent version, you will have to install the appropriate version from a llvm-toolchain-* source (the ubuntu-toolchain-r-test source must also be pulled in for dependency resolution); see below for examples:

matrix:
  include:
    # works on Precise and Trusty
    - os: linux
      addons:
        apt:
          sources:
            - ubuntu-toolchain-r-test
            - llvm-toolchain-precise-3.6
          packages:
            - clang-3.6
      env:
        - MATRIX_EVAL="CC=clang-3.6 && CXX=clang++-3.6"

    # works on Precise and Trusty
    - os: linux
      addons:
        apt:
          sources:
            - ubuntu-toolchain-r-test
            - llvm-toolchain-precise-3.7
          packages:
            - clang-3.7
      env:
        - MATRIX_EVAL="CC=clang-3.7 && CXX=clang++-3.7"

    # works on Precise and Trusty
    - os: linux
      addons:
        apt:
          sources:
            - ubuntu-toolchain-r-test
            - llvm-toolchain-precise-3.8
          packages:
            - clang-3.8
      env:
        - MATRIX_EVAL="CC=clang-3.8 && CXX=clang++-3.8"

    # works on Trusty
    - os: linux
      addons:
        apt:
          sources:
            - llvm-toolchain-trusty-3.9
          packages:
            - clang-3.9
      env:
        - MATRIX_EVAL="CC=clang-3.9 && CXX=clang++-3.9"

    # works on Trusty
    - os: linux
      addons:
        apt:
          sources:
            - llvm-toolchain-trusty-4.0
          packages:
            - clang-4.0
      env:
        - MATRIX_EVAL="CC=clang-4.0 && CXX=clang++-4.0"

    # works on Trusty
    - os: linux
      addons:
        apt:
          sources:
            - llvm-toolchain-trusty-5.0
          packages:
            - clang-5.0
      env:
        - MATRIX_EVAL="CC=clang-5.0 && CXX=clang++-5.0"

before_install:
    - eval "${MATRIX_EVAL}"

On OS X, the version of clang is controlled by the choice of osx_image.

CMake #

Ubuntu 12.04 ships with cmake 2.8.7 and Ubuntu 14.04 ships with cmake 3.9.2.

You can upgrade cmake to 3.2.3 on Precise from the george-edison55-precise-backports source (note that the cmake-data package contains dependencies which Aptitude does not automatically resolve), c.f.

addons:
  apt:
    sources:
      - george-edison55-precise-backports
    packages:
      - cmake-data
      - cmake

On OS X, the version of cmake is controlled by the choice of osx_image.