起因是因为我在对我的 C++ 项目进行跨平台适配,从 macOS 平台移植到 Windows 平台时,在使用 Cmake + MSVC 编译后,出现了这个问题。
问题原因
这是由于 Windows 平台默认使用的是 GBK 编码,而 macOS 平台上使用的是 UTF-8 编码。
解决方法
方法一
在 CMakeLists.txt 文件中添加如下代码:
add_compile_options("$<$<C_COMPILER_ID:MSVC>:/source-charset:utf-8>")
方法二
在 CMakeLists.txt 文件中添加如下代码:
if(MSVC)
target_compile_options(<你的项目名> PRIVATE "/utf-8")
endif()