I'd like to use C 20 in MSVC but I can't use the new char8_t in my project. MSVC provides a flag to disable char8_t: /Zc:char8_t-. This works fine if I use a MSVC solution project but it is ignored somehow in my CMAKE project and the compilation fails.
My CMakeLists.txt:
cmake_minimum_required (VERSION 3.12)
project ("CmakeTest")
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Zc:char8_t-")
add_executable (CmakeTest "CmakeTest.cpp" )
CMakeTest.cpp
#include <filesystem>
int main()
{
std::filesystem::path path;
std::string test = path.u8string();
return 0;
}
Until C 17 u8string() returned a std::string, so this compiled fine. But since C 20 it returns a std::u8string and the compilation fails. The flag "/Zc:char8_t-" should return to the old functionality, but the compilation still fails with the same error that there is no conversion possible from std::u8string to std::string.
The resulting call to cl.exe looks fine and includes the flag:
C:\PROGRA~1\MIB055~1\2022\COMMUN~1\VC\Tools\MSVC\1434~1.319\bin\Hostx64\x64\cl.exe /nologo /TP /DWIN32 /D_WINDOWS /W3 /GR /EHsc /Zc:char8_t- /MDd /Zi /Ob0 /Od /RTC1 -std:c 20 /showIncludes /FoCMakeFiles\CmakeTest.dir\CmakeTest.cpp.obj /FdCMakeFiles\CmakeTest.dir\ /FS -c D:\Projects\cmaketest\CmakeTest.cpp
Executing it manually leads to the same error.
CodePudding user response:
Instead of using CMAKE_CXX_STANDARD I am specifying "-std:c 20" manually. I think this is more of workaround but it works.
CMakeLists.txt
cmake_minimum_required (VERSION 3.12)
project ("CmakeTest")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std:c 20 /Zc:char8_t-")
add_executable (CmakeTest "CmakeTest.cpp" )