# Different between "-DCMAKE\_CXX\_COMPILER=clang++" and "-DCMAKE\_CXX\_COMPILER=clang-12"

**URL:** <https://discourse.cmake.org/t/different-between-dcmake-cxx-compiler-clang-and-dcmake-cxx-compiler-clang-12/3245>\
**Category:** Code\
**Created:** [May 2, 2021, 5:19am UTC](https://discourse.cmake.org/t/different-between-dcmake-cxx-compiler-clang-and-dcmake-cxx-compiler-clang-12/3245 "2021-05-02T05:19:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![psionic12](https://discourse.cmake.org/user_avatar/discourse.cmake.org/psionic12/32/998_2.png) [@psionic12](https://discourse.cmake.org/u/psionic12)\
**Post date:** [May 2, 2021, 5:19am UTC](https://discourse.cmake.org/t/different-between-dcmake-cxx-compiler-clang-and-dcmake-cxx-compiler-clang-12/3245/1 "2021-05-02T05:19:34Z")

</div>

In my environmental, `clang` and `clang++` are all symbolic links to `clang-12`. And if I use `-DCMAKE_CXX_COMPILER=clang++`, my C++ codes will linked to stdc++ without problem, but if I use `-DCMAKE_CXX_COMPILER=clang-12`, it will report errors like this:

```
Undefined symbols for architecture x86_64:
...
"std:: __1::basic_ostream<char, std::__ 1::char_traits<char> >::sentry::~sentry()", referenced from:
      std:: __1::basic_ostream<char, std::__ 1::char_traits<char> >& std:: __1::__ put_character_sequence<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, char const*, unsigned long) in main.cpp.o
...

```

According to my experience, the compiler (and linker) seems treat my project as a C project, not a C++ one.

So I’m curious what’s the different between `-DCMAKE_CXX_COMPILER=clang++`and `-DCMAKE_CXX_COMPILER=clang-12`? Why the former works well but the latter doesn’t, since they are the same (which is clang-12)?

What’s more, I’m using an IDE, which will “cleverly” change ` -DCMAKE_CXX_COMPILER=clang++` to `-DCMAKE_CXX_COMPILER=clang-12`. So in this case, what can I do in writing CMake code to make CMake treat my project as a C++? (I tried `project(untitled2 CXX)` but it doesn’t work).

P.S. I’m using macOs, not sure if this happens on linux.

---

<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:** [May 2, 2021, 10:38am UTC](https://discourse.cmake.org/t/different-between-dcmake-cxx-compiler-clang-and-dcmake-cxx-compiler-clang-12/3245/2 "2021-05-02T10:38:48Z")

</div>

The IDE is wrong. Clang (and GCC) change behavior based on the name by which it is executed. So `clang-12` will act as a C compiler and link the C runtime by default. `clang++-12` will act as a C++ compiler and link the C++ runtime by default. When compiling C++ with a C compiler (which generally works because $reasons), the C++ standard library must be linked manually.

---

<div class="post-metadata">

**Author:** ![psionic12](https://discourse.cmake.org/user_avatar/discourse.cmake.org/psionic12/32/998_2.png) [@psionic12](https://discourse.cmake.org/u/psionic12)\
**Post date:** [May 2, 2021, 11:37am UTC](https://discourse.cmake.org/t/different-between-dcmake-cxx-compiler-clang-and-dcmake-cxx-compiler-clang-12/3245/3 "2021-05-02T11:37:50Z")

</div>

Thanks for helping.

Do you mean that compiler will check the first argument passed in (which is the bin name), to decide to use C or C++ logic?

By the way, I tried to link C++ standard library manually by using `set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -stdlib=libc++")` but still not working, do you happen to know how to manually set the C++ standard library?

---

<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:** [May 2, 2021, 1:51pm UTC](https://discourse.cmake.org/t/different-between-dcmake-cxx-compiler-clang-and-dcmake-cxx-compiler-clang-12/3245/4 "2021-05-02T13:51:54Z")

</div>

> [@psionic12](#):
>
> Do you mean that compiler will check the first argument passed in (which is the bin name), to decide to use C or C++ logic?

Yes, compilers will generally do this.

> [@psionic12](#):
>
> do you happen to know how to manually set the C++ standard library?

It depends on the compiler and stdlib in question. You probably need to explicitly say `-lc++` or something.
