38

Is it possible to get Travis CI working with Clang that is capable of C++11? (I want Clang, not GCC, I already have GCC 4.8 working in Travis CI.) It appears that the version that is there pre-installed is not C++11 capable. All my attempts at installing any newer version end up failing because of this:

In file included from /usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/bits/move.h:57:   
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/type_traits:269:39: error:
use of undeclared identifier '__float128'
struct __is_floating_point_helper<__float128>

I have seen the -D__STRICT_ANSI__ trick but that clashes with other things for me.

Is it possible to get it working? See also my .travis.yml.

Community
  • 1
  • 1
wilx
  • 17,697
  • 6
  • 59
  • 114
  • awesome if you get it done =). by the way, other people are starting to use [BuildHive](https://buildhive.cloudbees.com/), maybe it's friendlier [for C++](http://wiki.cloudbees.com/bin/view/DEV/C+and+C%2B%2B+Builds) – oblitum Mar 02 '14 at 03:16
  • Personally I'd like to know how, if you get this figured out. Answering your own answer maybe. – oblitum Mar 03 '14 at 19:06
  • Related issues: https://github.com/travis-ci/travis-ci/issues/1379, https://github.com/travis-ci/travis-ci/issues/979 – Ciro Santilli OurBigBook.com Apr 18 '15 at 06:37

3 Answers3

61

There is now a better way to do this.

sudo: false
dist: trusty
language: cpp
os:
  - linux
compiler:
  - gcc
  - clang
install:
# /usr/bin/gcc is 4.6 always, but gcc-X.Y is available.
- if [[ $CXX = g++ ]]; then export CXX="g++-4.9" CC="gcc-4.9"; fi
# /usr/bin/clang has a conflict with gcc, so use clang-X.Y.
- if [[ $CXX = clang++ ]]; then export CXX="clang++-3.5" CC="clang-3.5"; fi
addons:
  apt:
    sources:
    - ubuntu-toolchain-r-test
    - llvm-toolchain-precise-3.5 # not sure why we needed this
  packages:
    - gcc-4.9
    - g++-4.9
    - clang-3.5

(The explicit sudo: false will let it build in Docker (for speed) even if you have a pre-docker repo, according to Travis support.)

Thanks to solarce at Travis support for noticing my error and fixing the docs.

Community
  • 1
  • 1
cdunn2001
  • 17,657
  • 8
  • 55
  • 45
  • What about clang++ with c++11 support? – Jared Burrows Jun 27 '15 at 05:45
  • See the [whitelist](https://github.com/travis-ci/apt-package-whitelist/blob/master/ubuntu-precise) and [clang version compatibility](http://clang.llvm.org/cxx_status.html). – cdunn2001 Jun 28 '15 at 08:02
  • Thank you. I see the white list does not have clang++. So I am guessing c++11 with clang is not available for travisci? I am manually setting up libc++. – Jared Burrows Jun 28 '15 at 17:06
  • 2
    We are using C++11 and we build for both g++ and clang++. See `-std=c++11` in [one of our Travis builds](https://travis-ci.org/pb-cdunn/pbdagcon/builds/68746420). The top-line of the Travis log indicates a dockerized build. You can examine [`.travis.yml`](https://github.com/pb-cdunn/pbdagcon/blob/bc1814dcb2f2ef15176e89ae8437cb1dc0993553/.travis.yml) for that build also. – cdunn2001 Jun 29 '15 at 06:06
  • Thank you so much for sharing. I guess I will need to upgrade my `.travis.yml` files again. – Jared Burrows Jun 29 '15 at 06:43
  • hey, thank you for your help. I got it working. I am hoping that they continue their apt support. – Jared Burrows Jul 06 '15 at 03:56
  • Well, I only got clang-3.0 working with gcc-x.y in Travis via APT. I cannot select a clang version without errors. But I think the dockerized build is worth it. @JaredBurrows, if you got another clang version working, please post your latest Travis build. – cdunn2001 Jul 12 '15 at 17:03
  • Via manual installation I have gcc 5 and clang 3.6 working here: https://github.com/jaredsburrows/OpenVirus/blob/master/.travis.yml. – Jared Burrows Jul 12 '15 at 18:22
  • Oh, you've lost the dockerized build. `sudo: required`. Yes, there are lots of ways with sudo. Look at the top of your Travis output. You will not see 'docker' in the name of the build machine. – cdunn2001 Jul 12 '15 at 19:02
  • My goal was just to compile it with gcc and clang with c++11 enabled. With your changes, I have have it on another branch: https://github.com/jaredsburrows/OpenVirus/blob/test/.travis.yml – Jared Burrows Jul 12 '15 at 19:10
  • 1
    Set the CC & CPP flags in the case of `before_install:` in cases such as `node_js` to avoid overwriting the standard `install:` commands. Saved the day for testing https://github.com/audreyt/node-webworker-threads on Node 4.x. – brodybits Oct 13 '15 at 09:54
33

Here is a part of my .travis.yml files (mostly taken from this file).

language: cpp

compiler:
  - clang
  - gcc

before_install:
  # g++4.8.1
  - if [ "$CXX" == "g++" ]; then sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test; fi

  # clang 3.4
  - if [ "$CXX" == "clang++" ]; then sudo add-apt-repository -y ppa:h-rayflood/llvm; fi

  - sudo apt-get update -qq

install:
  # g++4.8.1
  - if [ "$CXX" = "g++" ]; then sudo apt-get install -qq g++-4.8; fi
  - if [ "$CXX" = "g++" ]; then export CXX="g++-4.8"; fi

  # clang 3.4
  - if [ "$CXX" == "clang++" ]; then sudo apt-get install --allow-unauthenticated -qq clang-3.4; fi
  - if [ "$CXX" == "clang++" ]; then export CXX="clang++-3.4"; fi

script: 
  - $CXX --version

EDIT because it can be very useful to add libc++ for travis. Up to my knowledge, there is no Linux package for libc++, so one has to compile it "by hand". Do not forget -stdlib=libc++ in CXXFLAGS while compiling with clang.

install:
  # clang 3.4
  - if [ "$CXX" == "clang++" ]; then sudo apt-get install --allow-unauthenticated -qq clang-3.4; fi
  - if [ "$CXX" == "clang++" ]; then export CXXFLAGS="-std=c++0x -stdlib=libc++"; fi
  - if [ "$CXX" == "clang++" ]; then svn co --quiet http://llvm.org/svn/llvm-project/libcxx/trunk libcxx; fi

  - if [ "$CXX" == "clang++" ]; then cd libcxx/lib && bash buildit; fi
  - if [ "$CXX" == "clang++" ]; then sudo cp ./libc++.so.1.0 /usr/lib/; fi
  - if [ "$CXX" == "clang++" ]; then sudo mkdir /usr/include/c++/v1; fi
  - if [ "$CXX" == "clang++" ]; then cd .. && sudo cp -r include/* /usr/include/c++/v1/; fi
  - if [ "$CXX" == "clang++" ]; then cd /usr/lib && sudo ln -sf libc++.so.1.0 libc++.so; fi
  - if [ "$CXX" == "clang++" ]; then sudo ln -sf libc++.so.1.0 libc++.so.1 && cd $cwd; fi

  - if [ "$CXX" == "clang++" ]; then export CXX="clang++-3.4"; fi
Florian Richoux
  • 1,543
  • 1
  • 14
  • 28
  • Does this still work for you? No matter what I try, my build fails on Travis with `'iostream' file not found`. – Jared Burrows May 10 '15 at 17:49
  • @JaredBurrows I didn't touch travis for 9 months, but I just run a test and yes, it still work today. https://travis-ci.org/richoux/Wall-in/jobs/61990830 Are you sure you give the following clang option `-stdlib=libc++` – Florian Richoux May 10 '15 at 18:12
  • Thank you for posting this. I will compare your build with mine. – Jared Burrows May 10 '15 at 18:35
1

It appears that the clang developers fixed this when you build in -std=gnu++11 mode.

Are you able to build with that flag instead of __STRICT_ANSI__?

Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
  • So instead of `-std=c++11 -stdlib=libc++` we can use `-std=gnu++11` or `-std=gnu++11 -stdlib=libc++`? – Jared Burrows May 10 '15 at 17:50
  • @JaredBurrows: If you are using libc++, you wouldn't see this bug at all. This bug is due to a conflict between clang and libstdc++. – Bill Lynch May 10 '15 at 18:21
  • 1
    I am trying to get a project to build on TravisCI that uses C and C++. It compiles fine with gcc and clang(locally) but not on TravisCI, clang fails. I am using cmake. It looks like for TravisCI am missing the libraries according to Florian's answer. – Jared Burrows May 10 '15 at 18:28