From e2b6dc48b1b40c869619cf3cd071e9c768f10535 Mon Sep 17 00:00:00 2001 From: fghzxm Date: Wed, 22 Feb 2023 20:00:07 +0800 Subject: [PATCH] build: set /utf-8 flag when using MSVC (#4975) MSVC by default decodes source files in the current Windows code page if they don't have a Unicode BOM, and also encodes strings and chars into the current code page before storing them into the compiled binary. Our files are always encoded in UTF-8, and our code always assumes runtime strings are encoded in UTF-8, so we should pass the `/utf-8` flag to MSVC. Microsoft Docs: https://learn.microsoft.com/en-us/cpp/build/reference/utf-8-set-source-and-executable-character-sets-to-utf-8 --- CMakeLists.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index cce63777e..2d7f9e32f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -181,6 +181,8 @@ if(WIN32) # Ignore various deprecation and security warnings (at least for now) set(CMAKE_${L}_FLAGS "${CMAKE_${L}_FLAGS} -D_CRT_SECURE_NO_DEPRECATE -D_CRT_NONSTDC_NO_DEPRECATE -D_SCL_SECURE_NO_WARNINGS -D_WINSOCK_DEPRECATED_NO_WARNINGS") if(MSVC) + # Set source file encoding and execution charset to UTF-8 + set(CMAKE_${L}_FLAGS "${CMAKE_${L}_FLAGS} /utf-8") # Reduce noise (at least for now) set(CMAKE_${L}_FLAGS "${CMAKE_${L}_FLAGS} /wd4244 /wd4267") # Make caching-friendly (store debug info inside object files)