Manu343726 Manu343726 - 13 days ago 7
C++ Question

Change C++ compiler for cmake under TravisCI

As far I know the best way to change the target C++ compiler used by cmake is to change the

CXX
environment variable just before the call to cmake:

$ export CXX="clang++" && cmake --build


The Travis CI build sets the
CXX
and
CC
accordingly to the settings of the build. So if you have this in your
.travis.yml
:

language: cpp
compiler:
- gcc
- clang

script:
- cmake --build
- ./bin/foo


The first time
cmake
should use GCC and Clang on the latter isn't?

Instead, the GCC build compiles just fine (Its the default compiler used by cmake), but the Clang version uses GCC too:


0.01s$ echo $CC $CXX

clang clang++

The command "echo $CC $CXX" exited with 0.

0.02s$ $CXX --version

clang version 3.4 (tags/RELEASE_34/final) Target: x86_64-unknown-linux-gnu Thread model: posix

Running: cmake -G "Unix Makefiles" -Wno-dev ../cmake

-- The C compiler identification is GNU

-- The CXX compiler identification is GNU

-- Check for working C compiler: /usr/bin/gcc

-- Check for working C compiler: /usr/bin/gcc -- works

-- Detecting C compiler ABI info

-- Detecting C compiler ABI info - done

-- Check for working CXX compiler: /usr/bin/c++

-- Check for working CXX compiler: /usr/bin/c++ -- works

-- Detecting CXX compiler ABI info

-- Detecting CXX compiler ABI info - done


I even tried to set those variables again just before the call:

- if [ "$CXX" == "clang++" ]; then export CXX="clang++" && cmake --build; fi
- if [ "$CXX" == "g++" ]; then export CXX="g++" && cmake --build; fi


Which has no much sense I think...

Here is the successful build using GCC, and here the (Supposed to be) clang build.

My question is: How I can change the compiler used by cmake under Travis CI?

Here is my .travis.yml.

Answer

Explanation

In your .travis.yml we see:

- if [ "$CXX" == "clang++" ]; then export CXX="clang++" && bii cpp:build; fi
- if [ "$CXX" == "g++" ];     then export CXX="g++"     && bii cpp:build; fi

biicode's command bii cpp:build runs CMake with biicode's default generator which is "Unix Makefiles" for UNIX platform (GNU toolchain). Read about it: Biicode C++ documentation - Generators and IDEs.

This behaviour is seen in your logs: Running: cmake -G "Unix Makefiles" -Wno-dev ../cmake (https://travis-ci.org/Manu343726/Turbo/jobs/33889114, line 257)

CMake not always looks for environment variables CXX/CC (as stated in CMake FAQ). It depends on selected generator. It works for me when I call CMake with no generator (my travis file: https://github.com/quepas/Graph-ene/blob/master/.travis.yml).

Solutions

Comments