Build a C++ Project
For Language versions and other build-environment specific information visit our reference pages:
This guide covers build environment and configuration topics specific to C++ projects. Please make sure to read our Onboarding 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 build configuration guide to learn more.
Test against Compilers #
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, the Travis CI C++ builder will export the CXX
and
CXX_FOR_BUILD
env variables to point to either g++
or clang++
, and
correspondingly export the CC
and CC_FOR_BUILD
env variables to point
to either gcc
or clang
.
On macOS, gcc
is an alias for clang
, and g++
is an alias for clang++
.
Set a specific GCC version to use GCC on macOS.
Examples #
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 variables $CC
and $CXX
are known to interfere with MPI projects.
In this case, we recommend unsetting it:
before_install:
- test -n $CC && unset CC
- test -n $CXX && unset CXX
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 #
- Precise ships with GCC 4.6.3
- Trusty ships with GCC 4.8.2
- Xenial ships with GCC 5.4.0
- Bionic ships with GCC 7.4.0
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 5.1 (the C++ language support was feature-complete in 4.8.1 but the standard library didn’t support all C++11 features until later, in particular 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 macOS #
On macOS, 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}"
GCC on FreeBSD #
Travis CI FreeBSD image ships with GCC 10.0.0 (it’s not in the base system by default).
To upgrade GCC to a more recent version, install the appropriate version from packages; see below for examples:
os: freebsd
addons:
pkg:
- gcc10
env:
- CC=gcc10
- CXX=g++10
Clang on Linux #
- Precise ships with Clang 3.4
- Trusty ships with Clang 3.5.0
- Xenial ships with Clang 7
- Bionic ships with Clang 7
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}"
Clang on macOS #
On macOS, the version of clang
is controlled by the choice of osx_image
.
You can find here the list of available osx_image
.
matrix:
include:
- os: osx
osx_image: xcode10.1
- os: osx
osx_image: xcode9.4
You can find the clang
version shipped by Xcode here.
Clang on FreeBSD #
Clang is the default compiler on FreeBSD
FreeBSD ships with Clang 8.0.1
To upgrade Clang to a more recent version, install the appropriate version from packages; see below for examples:
os: freebsd
addons:
pkg:
- llvm90
env:
- CC=/usr/local/bin/clang90 # llvm90 installs it to /usr/local/bin/clang90
- CXX=/usr/local/bin/clang++90 # llvm90 installs it to /usr/local/bin/clang++90
Clang is the default compiler on FreeBSD
CMake #
- Precise ships with CMake 2.8.7
- Trusty ships with CMake 3.9.2
- Xenial ships with CMake 3.12.4
- Bionic ships with CMake 3.12.4
- FreeBSD ships with CMake 3.15.5
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 macOS, the version of cmake
is controlled by the choice of osx_image
.