Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setup Clang on Travis CI for C++17

I have a small C++17 project for which I want to setup Travis CI. Since it is C++17 it requires modern compilers; I settled for gcc-7 and clang-6. While the gcc build compiles and links just fine, I can't, for the life of me, figure out how to setup clang properly. It looks like it always uses the standard library implementation of the very outdated pre-installed gcc instead of its own.

The travis log shows the following lines on cmake --build . -- VERBOSE=1:

/usr/bin/clang-6.0 -I/home/travis/build/myuser/perlin/include -g -std=gnu++1z -o CMakeFiles/perlin.dir/main.cpp.o -c /home/travis/build/myuser/perlin/main.cpp

In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/4.9/../../../../include/c++/4.9/algorithm:62: In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/4.9/../../../../include/c++/4.9/bits/stl_algo.h:66: In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/4.9/../../../../include/c++/4.9/random:38: In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/4.9/../../../../include/c++/4.9/cmath:44: /home/travis/build/myuser/perlin/include/math.h:48:28: error: no template named 'is_arithmetic_v' in namespace 'std'; did you mean 'is_arithmetic'?

Again, it builds fine on gcc. I also double checked that all required headers are included.

My .travis.yml looks like this:

language: cpp
dist: trusty

matrix:
  include:
  - os: linux
    addons:
      apt:
        sources:
        - ubuntu-toolchain-r-test
        packages:
        - g++-7
        - libpng-dev
    env:
    - MATRIX_EVAL="CC=gcc-7 && CXX=g++-7"
    - LINKER_FLAGS=""

  - os: linux
    addons:
      apt:
        sources:
        - ubuntu-toolchain-r-test
        - llvm-toolchain-trusty-6.0
        - sourceline: 'deb http://apt.llvm.org/trusty/ llvm-toolchain-trusty-6.0 main'
          key_url: 'https://apt.llvm.org/llvm-snapshot.gpg.key'
        packages:
        - clang-6.0
        - libstdc++6
        - libpng-dev
    env:
    - MATRIX_EVAL="CC=clang-6.0 && CXX=clang-6.0"
    - LINKER_FLAGS="-stdlib=libc++ -lc++abi"

before_install:
- eval "${MATRIX_EVAL}"

script:
- cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_EXE_LINKER_FLAGS=${LINKER_FLAGS} .
- cmake --build . -- VERBOSE=1

The CMakeLists.txt:

cmake_minimum_required(VERSION 3.9)
project(perlin)

set(CMAKE_CXX_STANDARD 17)

if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -pedantic")
endif()

add_library(noise INTERFACE)
target_sources(noise INTERFACE
        ${PROJECT_SOURCE_DIR}/include/seamless_noise_generator_2d.h
        ${PROJECT_SOURCE_DIR}/include/fractal_noise_generator.h
        ${PROJECT_SOURCE_DIR}/include/perlin_noise_generator.h
        ${PROJECT_SOURCE_DIR}/include/point.h
        ${PROJECT_SOURCE_DIR}/include/vector.h
        ${PROJECT_SOURCE_DIR}/include/math.h)
target_include_directories(noise INTERFACE include)

find_package(PNG)
if (PNG_FOUND)
    add_executable(noise_test main.cpp)
    target_link_libraries(noise_test ${PNG_LIBRARY} noise m)
else()
    message(info "Did not find libpng. Not building test executable.")
endif ()

If anyone knows what I'm doing wrong here, help would be very appreciated.

like image 539
Drag-On Avatar asked Jul 21 '18 14:07

Drag-On


1 Answers

I could notice these issues:

  • CXX should point to the C++ driver, that is: clang++-6.0.
  • You should specify -stdlib=libc++ in the CXXFLAGS as well.

The latter seems to be the culprit.

If not specified otherwise, clang++ defaults to stdlib++ for standard C++ headers.
The build output suggests that headers are indeed picked from gcc-4.9, which is most likely the system gcc. That explains the build failure, since gcc-4.9 is quite old and doesn't support C++17 (changelog suggests that it has experimental C++14 support). is_arithmetic_v, which fails the build, is indeed new C++17 addition.

like image 108
valiano Avatar answered Nov 14 '22 23:11

valiano