简体   繁体   中英

c++20, clang 13.0.0, u8string support

I'm using clang 13.0.0 in a CMAKE-based project, CMAKE_CXX_STANDARD is defined as 20. The following code causes a compilation error (no type named 'u8string' in namespace 'std'):

#include <iostream>
#include <string>

int main() {
#ifdef __cpp_char8_t
    std::u8string sss = u8"a";  // <---- this branch is picked up
#else
    std::string sss = "b"
#endif    
return 0;
}

Below is the CMakeLists.txt:

cmake_minimum_required(VERSION 3.20)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_C_COMPILER clang)
set(CMAKE_CXX_COMPILER clang++)

project(clang_test)

add_executable(clang_test main.cpp)

Am I missing something or is it a clang bug? I mean, if std::u8string is not yet there, the macro shouldn't be defined, right? By the way, if you know the way to undefine it somehow with the CMake, please, share your experience. Here the topic starter faces the same problem, I think, but there is no solution proposed yet.

Update: the platform is RHEL 8.4

Update 2: moved the 'project' call below the compiler settings in the CMakeLists.txt

Frank's suggestion was correct - the problem is in that I link with libstdc++ and not libc++;

target_compile_options(clang_test PRIVATE -stdlib=libc++) 

fixes the issue for me.

Also, Tsyvarev's point is indeed a very important one, and the compiler should be set before the project call. I've corrected the example in my question.

Thanks a lot for your help!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM