# How does CMake find compilers?

**URL:** https://discourse.cmake.org/t/how-does-cmake-find-compilers/3009
**Category:** Usage
**Created:** [March 25, 2021, 1:19am UTC](https://discourse.cmake.org/t/how-does-cmake-find-compilers/3009 "2021-03-25T01:19:14Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![anon45792294](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/a587f6/32.png) [@anon45792294](https://discourse.cmake.org/u/anon45792294)
#### Post date: [March 25, 2021, 1:19am UTC](https://discourse.cmake.org/t/how-does-cmake-find-compilers/3009/1 "2021-03-25T01:19:14Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [March 25, 2021, 6:21pm UTC](https://discourse.cmake.org/t/how-does-cmake-find-compilers/3009/2 "2021-03-25T18:21:26Z")

</div>

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`

---

<div class="post-metadata">

### Author: ![YMba9g8j9CJp0wLoQf5y](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ymba9g8j9cjp0wloqf5y/32/1244_2.png) [@YMba9g8j9CJp0wLoQf5y](https://discourse.cmake.org/u/YMba9g8j9CJp0wLoQf5y)
#### Post date: [March 25, 2021, 11:28pm UTC](https://discourse.cmake.org/t/how-does-cmake-find-compilers/3009/3 "2021-03-25T23:28:14Z")

</div>

What takes precedence PATH or CXX?

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [March 25, 2021, 11:49pm UTC](https://discourse.cmake.org/t/how-does-cmake-find-compilers/3009/4 "2021-03-25T23:49:37Z")

</div>

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