简体   繁体   English

c++20、clang 13.0.0、u8string 支持

[英]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'):我在基于 CMAKE 的项目中使用 clang 13.0.0,CMAKE_CXX_STANDARD 定义为 20。以下代码导致编译错误(命名空间“std”中没有名为“u8string”的类型):

#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:下面是 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?我的意思是,如果 std::u8string 还不存在,则不应定义宏,对吗? By the way, if you know the way to undefine it somehow with the CMake, please, share your experience.顺便说一句,如果您知道使用 CMake 以某种方式取消定义它的方法,请分享您的经验。 Here the topic starter faces the same problem, I think, but there is no solution proposed yet.我认为这里的主题 starter 面临同样的问题,但还没有提出解决方案。

Update: the platform is RHEL 8.4更新:平台为 RHEL 8.4

Update 2: moved the 'project' call below the compiler settings in the CMakeLists.txt更新 2:将“项目”调用移至 CMakeLists.txt 中的编译器设置下方

Frank's suggestion was correct - the problem is in that I link with libstdc++ and not libc++;弗兰克的建议是正确的——问题在于我链接的是 libstdc++ 而不是 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.另外, Tsyvarev 的观点确实是非常重要的一点,编译器应该在项目调用之前设置。 I've corrected the example in my question.我已经更正了我的问题中的例子。

Thanks a lot for your help!非常感谢你的帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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