Travis CI with Clang 3.4 and C++11

12,199

Solution 1

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

Solution 2

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.

Solution 3

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__?

Share:
12,199
wilx
Author by

wilx

(my about me is currently blank)

Updated on June 15, 2022

Comments

  • wilx
    wilx almost 2 years

    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.

  • Jared Burrows
    Jared Burrows about 9 years
    Does this still work for you? No matter what I try, my build fails on Travis with 'iostream' file not found.
  • Jared Burrows
    Jared Burrows about 9 years
    So instead of -std=c++11 -stdlib=libc++ we can use -std=gnu++11 or -std=gnu++11 -stdlib=libc++?
  • Florian Richoux
    Florian Richoux about 9 years
    @JaredBurrows I didn't touch travis for 9 months, but I just run a test and yes, it still work today. travis-ci.org/richoux/Wall-in/jobs/61990830 Are you sure you give the following clang option -stdlib=libc++
  • Bill Lynch
    Bill Lynch about 9 years
    @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++.
  • Jared Burrows
    Jared Burrows about 9 years
    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
    Jared Burrows about 9 years
    Thank you for posting this. I will compare your build with mine.
  • Jared Burrows
    Jared Burrows almost 9 years
    What about clang++ with c++11 support?
  • cdunn2001
    cdunn2001 almost 9 years
  • Jared Burrows
    Jared Burrows almost 9 years
    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++.
  • cdunn2001
    cdunn2001 almost 9 years
    We are using C++11 and we build for both g++ and clang++. See -std=c++11 in one of our Travis builds. The top-line of the Travis log indicates a dockerized build. You can examine .travis.yml for that build also.
  • Jared Burrows
    Jared Burrows almost 9 years
    Thank you so much for sharing. I guess I will need to upgrade my .travis.yml files again.
  • Jared Burrows
    Jared Burrows almost 9 years
    hey, thank you for your help. I got it working. I am hoping that they continue their apt support.
  • cdunn2001
    cdunn2001 almost 9 years
    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.
  • Jared Burrows
    Jared Burrows almost 9 years
    Via manual installation I have gcc 5 and clang 3.6 working here: github.com/jaredsburrows/OpenVirus/blob/master/.travis.yml.
  • cdunn2001
    cdunn2001 almost 9 years
    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.
  • Jared Burrows
    Jared Burrows almost 9 years
    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: github.com/jaredsburrows/OpenVirus/blob/test/.travis.yml
  • brodybits
    brodybits over 8 years
    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 github.com/audreyt/node-webworker-threads on Node 4.x.