Andrew Spott
Andrew Spott

Reputation: 3627

Make with multiple compilers

When using C++14/C++11 features and STL includes, GCC and clang have different behaviors. Libc++ has a tendency to include things implicitly, while libstdc++ seems to have a tendency to require explicit includes. GCC -Wpedantic seems to be a little more pedantic than clang -Wpedantic.

What I want is to have a makefile that attempts to build with both of these compilers, in order to catch errors. What is the best way to do this?

Upvotes: 0

Views: 1170

Answers (1)

o11c
o11c

Reputation: 16056

As a general rule, make's job is only to test with one particular set of options. Testing multiple configurations is the job of some higher-level script.

First, set up your project so that it can do out-of-tree builds. Usually a script named configure is used to do this.

Then, for each configuration, create an out-of-tree build:

pushd; mkdir build-gcc-libstdc++ && cd build-gcc-libstdc++ && ../configure CXX='g++'; popd
pushd; mkdir build-gcc-libc++ && cd build-gcc-libc++ && ../configure CXX='g++-libc++'; popd
pushd; mkdir build-clang-libstdc++ && cd build-clang-libstdc++ && ../configure CXX='clang++'; popd
pushd; mkdir build-clang-libc++ && cd build-clang-libc++ && ../configure CXX='clang++-libc++'; popd

(You may also want to do this for multiple gcc versions, since the headers vary a lot between them too).

To run them all, do:

make -C build-gcc-libstdc++ [targets ...] &&
make -C build-gcc-libc++ [targets ...] &&
make -C build-clang-libstdc++ [targets ...] &&
make -C build-clang-libc++ [targets ...]

but usually I just build with the default configuration and let travis catch errors from other configurations. Note that I don't use libc++: https://github.com/themanaworld/tmwa/blob/master/.travis.yml

Upvotes: 2

Related Questions