einpoklum
einpoklum

Reputation: 132260

How to get CMake to choose between multiple compilers?

Suppose I have a system with multiple C/C++ compilers - various versions of GCC, clang and ICC. Also suppose I have a CMake C/C++ project which has certain requirements and certain preferences regarding the C/C++ compiler to use; and to complicate things, suppose these requirements and preferences and generated dynamically based on the combination of project options I've set (with ccmake or otherwise).

Now, other answers about using a compiler other than the default suggest setting the CC or CXX environment variables - but this is clearly inappropriate here.

Is there a way to get CMake to:

  1. Detect the available compilers.
  2. Choose the one it likes based on some rules/ranking mechanism?

Notes:

Upvotes: 4

Views: 476

Answers (1)

usr1234567
usr1234567

Reputation: 23441

Historically, and probably also technically, the C compiler is very basic to the CMake run. Many commands rely on having a compiler, like detecting symbols or trying to compile a piece of code.

As far as I know, there is no way to tests multiple compilers and chose one. To get this, you have to

  • either wrap the CMake calls and have some logic outside which adds the different compilers to the CMake calls
  • or have to re-write a bunch of CMake functions for yourself.

My advice: Accept the way CMake works and teach it to your users.

Upvotes: 1

Related Questions