# Help with CC, Clang, Environment, and CMake

**URL:** https://discourse.cmake.org/t/help-with-cc-clang-environment-and-cmake/2221
**Category:** Usage
**Created:** [November 23, 2020, 3:03pm UTC](https://discourse.cmake.org/t/help-with-cc-clang-environment-and-cmake/2221 "2020-11-23T15:03:05Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![mathomp4](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mathomp4/32/176_2.png) [@mathomp4](https://discourse.cmake.org/u/mathomp4)
#### Post date: [November 23, 2020, 3:03pm UTC](https://discourse.cmake.org/t/help-with-cc-clang-environment-and-cmake/2221/1 "2020-11-23T15:03:05Z")

</div>

All,

I think I might be an idiot, but I’m not sure what’s going on. I’m having issues with CMake because it seems to be finding the “wrong clang”. In an autotools build (controlled by CMake/ExternalProject\_Add), it’s trying to run a program that is essentially:

```auto
#include <stdio.h>

int main() {
  printf("Hello world\n");
}

```

and it’s failing. Why? Because it’s calling the “wrong” clang. This works:

```auto
❯ /usr/bin/clang -o conftest -fPIC test.c
❯ echo $?
0

```

This does not:

```auto
❯ /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -o conftest -fPIC test.c
test.c:1:10: fatal error: 'stdio.h' file not found
#include <stdio.h>
         ^ ~~~~~~~~
1 error generated.
❯ echo $?
1

```

So, I start drilling down and I see some oddness. I made up this `CMakeLists.txt`:

```cmake
cmake_minimum_required(VERSION 3.17)
project(test VERSION 1.0.0 LANGUAGES C)
message(STATUS "CC from ENV: $ENV{CC}")
set(SERIAL_C_COMPILER ${CMAKE_C_COMPILER})
message(STATUS "SERIAL_C_COMPILER: ${SERIAL_C_COMPILER}")

```

Now I try and use it. First I load my module and echo out `CC` as set by the modulefile:

```auto
❯ ml purge
❯ ml intel-clang
❯ echo $CC
/usr/bin/clang

```

Now I run `cmake`:

```auto
❯ cmake ..
-- The C compiler identification is AppleClang 12.0.0.12000032
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- CC from ENV: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang
-- SERIAL_C_COMPILER: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/mathomp4/CmakeTest/build

```

Instead of using the `CC` from my environment, it’s using…a different one.

So is there a way for CMake to use what I have in `CC` for determining the C compiler? Or do I just always need to pass in `-DCMAKE_C_COMPILER=$CC` which seems to work:

```auto
❯ cmake .. -DCMAKE_C_COMPILER=$CC
-- The C compiler identification is AppleClang 12.0.0.12000032
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/clang - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- CC from ENV: /usr/bin/clang
-- SERIAL_C_COMPILER: /usr/bin/clang
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/mathomp4/CmakeTest/build

```

to have CMake use the `CC` in my environment?

Or is there a way to tell CMake don’t use the `/deep/inside/xcode/usr/bin/clang` but actually `/usr/bin/clang`?

---

<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: [November 25, 2020, 5:24pm UTC](https://discourse.cmake.org/t/help-with-cc-clang-environment-and-cmake/2221/2 "2020-11-25T17:24:02Z")

</div>

My guess is that you’ve install Xcode, but haven’t run it. Once run, Xcode installs some things so that the command line tools work as intended.

---

<div class="post-metadata">

### Author: ![mathomp4](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mathomp4/32/176_2.png) [@mathomp4](https://discourse.cmake.org/u/mathomp4)
#### Post date: [November 25, 2020, 9:33pm UTC](https://discourse.cmake.org/t/help-with-cc-clang-environment-and-cmake/2221/3 "2020-11-25T21:33:44Z")

</div>

@ben.boeckel No, I have Xcode and Xcode Command Line utilities installed. And if I use autotools outside of CMake (where it picks up `$CC` from the environment) it works just fine. It’s somehow CMake itself that is doing this resolving…

---

<div class="post-metadata">

### Author: ![friendlyanon](https://discourse.cmake.org/user_avatar/discourse.cmake.org/friendlyanon/32/970_2.png) [@friendlyanon](https://discourse.cmake.org/u/friendlyanon)
#### Post date: [November 25, 2020, 10:01pm UTC](https://discourse.cmake.org/t/help-with-cc-clang-environment-and-cmake/2221/4 "2020-11-25T22:01:01Z")

</div>

Probably worth looking at what `which cmake` prints and investigate what it is that you are actually running when invoking `cmake` in your shell.

---

<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: [November 25, 2020, 10:14pm UTC](https://discourse.cmake.org/t/help-with-cc-clang-environment-and-cmake/2221/5 "2020-11-25T22:14:27Z")

</div>

Some tracing of how the compiler is determined would be nice. `--trace-expand` might be able to shed some light on the CMake code portion of that at least.

Cc: @brad.king

---

<div class="post-metadata">

### Author: ![mathomp4](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mathomp4/32/176_2.png) [@mathomp4](https://discourse.cmake.org/u/mathomp4)
#### Post date: [November 25, 2020, 11:10pm UTC](https://discourse.cmake.org/t/help-with-cc-clang-environment-and-cmake/2221/6 "2020-11-25T23:10:00Z")

</div>

Here is the CMake info:

```auto
❯ which cmake
/Users/mathomp4/.homebrew/brew/bin/cmake
❯ cmake --version
cmake version 3.19.1

CMake suite maintained and supported by Kitware (kitware.com/cmake).

```

And I’m attaching the output of:

```auto
cmake .. --trace-expand |& tee trace.out

```

[trace.out](https://discourse.cmake.org/uploads/short-url/eF01Y0zurm8Cl5eGhD6TKItejsS.out) (1.5 MB)

---

<div class="post-metadata">

### Author: ![vre](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/v/f04885/32.png) [@vre](https://discourse.cmake.org/u/vre)
#### Post date: [November 26, 2020, 8:35am UTC](https://discourse.cmake.org/t/help-with-cc-clang-environment-and-cmake/2221/7 "2020-11-26T08:35:56Z")

</div>

[This thread](https://stackoverflow.com/questions/63342521/clang-on-macos-having-problems-with-its-includes) on StackOverflow may be of interest to you

---

<div class="post-metadata">

### Author: ![mathomp4](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mathomp4/32/176_2.png) [@mathomp4](https://discourse.cmake.org/u/mathomp4)
#### Post date: [November 30, 2020, 1:28pm UTC](https://discourse.cmake.org/t/help-with-cc-clang-environment-and-cmake/2221/8 "2020-11-30T13:28:35Z")

</div>

@vre Unfortunately, I did not build Clang myself. I’m using the Clang from XCode. And, just like C, I see the same issue with C++. If I expand my `CMakeLists.txt`:

```cmake
cmake_minimum_required(VERSION 3.17)
project(test VERSION 1.0.0 LANGUAGES C CXX)
message(STATUS "CC from ENV: $ENV{CC}")
set(SERIAL_C_COMPILER ${CMAKE_C_COMPILER})
message(STATUS "SERIAL_C_COMPILER: ${SERIAL_C_COMPILER}")
message(STATUS "CXX from ENV: $ENV{CXX}")
set(SERIAL_CXX_COMPILER ${CMAKE_CXX_COMPILER})
message(STATUS "SERIAL_CXX_COMPILER: ${SERIAL_CXX_COMPILER}")

```

```auto
❯ echo $CC $CXX
/usr/bin/clang /usr/bin/clang++
❯ cmake ..
-- The C compiler identification is AppleClang 12.0.0.12000032
-- The CXX compiler identification is AppleClang 12.0.0.12000032
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- CC from ENV: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang
-- SERIAL_C_COMPILER: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang
-- CXX from ENV: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++
-- SERIAL_CXX_COMPILER: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/mathomp4/CmakeTest/build

```

So, again, CMake is seeing the “long” CXX. And if I try a tester:

```auto
#include <ostream>
#include <algorithm>

int main(int argc, char** argv) {
return 0;
}

```

```auto
❯ /usr/bin/clang++ -o conftest ./test.cc
❯ echo $?
0
❯ /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -o conftest ./test.cc
In file included from ./test.cc:1:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/ostream:138:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/ios:214:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iosfwd:95:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/wchar.h:118:15: fatal error: 'wchar.h' file not
      found
#include_next <wchar.h>
              ^ ~~~~~~~~
1 error generated.
❯ echo $?
1

```

---

<div class="post-metadata">

### Author: ![brad.king](https://discourse.cmake.org/user_avatar/discourse.cmake.org/brad.king/32/11_2.png) [@brad.king](https://discourse.cmake.org/u/brad.king)
#### Post date: [November 30, 2020, 3:36pm UTC](https://discourse.cmake.org/t/help-with-cc-clang-environment-and-cmake/2221/9 "2020-11-30T15:36:19Z")

</div>

See [CMake Issue 19180](https://gitlab.kitware.com/cmake/cmake/-/issues/19180).

Currently CMake unwraps `/usr/bin/clang` to be the full path to the Clang inside the currently active Xcode. When launching the compiler without the wrapper, an explicit `-isysroot /path/to/macOS/SDK` is needed. CMake adds this, but other buildsystems may not.

A compounding factor is that CMake also sets the `CC` and `CXX` environment variables to match whatever compiler it resolved so that other configure scripts executed during the configuration via `execute_process` will see it. This historical behavior may be removed with a policy. See [CMake Issue 21378](https://gitlab.kitware.com/cmake/cmake/-/issues/21378).

---

<div class="post-metadata">

### Author: ![mathomp4](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mathomp4/32/176_2.png) [@mathomp4](https://discourse.cmake.org/u/mathomp4)
#### Post date: [December 1, 2020, 8:54pm UTC](https://discourse.cmake.org/t/help-with-cc-clang-environment-and-cmake/2221/10 "2020-12-01T20:54:13Z")

</div>

Hmm. Interesting. Well, I’ll see if I can figure out a way to inject it into the sub-autotools build.

As for the policy, do you know what the number is? I tried reading `man 7 cmake-policies` but I didn’t see it (might have got my search wrong).

---

<div class="post-metadata">

### Author: ![brad.king](https://discourse.cmake.org/user_avatar/discourse.cmake.org/brad.king/32/11_2.png) [@brad.king](https://discourse.cmake.org/u/brad.king)
#### Post date: [December 1, 2020, 9:14pm UTC](https://discourse.cmake.org/t/help-with-cc-clang-environment-and-cmake/2221/11 "2020-12-01T21:14:44Z")

</div>

There isn’t such a policy yet. The linked issue discusses it.

---

<div class="post-metadata">

### Author: ![mathomp4](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mathomp4/32/176_2.png) [@mathomp4](https://discourse.cmake.org/u/mathomp4)
#### Post date: [December 1, 2020, 9:26pm UTC](https://discourse.cmake.org/t/help-with-cc-clang-environment-and-cmake/2221/12 "2020-12-01T21:26:38Z")

</div>

Ohhh. Well I feel dumb. I thought that issue was referring to a policy already around. 🤦‍♂️
