How does CMake find compilers?

I’m a bit confused about the order of operations regarding finding the compilers.

There are so many ways to set the compiler in CMake.

You can set the environment variables CXX, CC.

You can set the variables CMAKE_C_COMPILER, CMAKE_CXX_COMPILER. Either as cache variables or non cache variables.

You can also set a CMAKE_TOOLCHAIN_FILE.

What’s the order of operations? What takes precedence? What’s the overall design philosophy guiding this?

1 Like

I think that it goes:

  • toolchain file is read first (if provided)
  • if CMAKE_<LANG>_COMPILER is unset, search for a compiler (usually via envvars) in PATH

What takes precedence PATH or CXX?

I imagine CXX (since it is more specific) and then CMake searches for known/common binary names in $PATH.