Different between "-DCMAKE_CXX_COMPILER=clang++" and "-DCMAKE_CXX_COMPILER=clang-12"

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.

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.

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?

Yes, compilers will generally do this.

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