设置Visual C ++编译器的执行字符集 [英] Set execution character set for Visual C++ compiler
问题描述
是否可以为Visual C ++编译器设置执行字符集?
Is it possible to set the execution character set for Visual C++ compiler?
当尝试将(UCN)字符串文字转换为宽字符串时,使用Visual Studio 2015进行编译时会发生运行时崩溃:
When trying to convert an (UCN) string literal to a wide string, a runtime crash happens when using Visual Studio 2015 for compilation:
std::string narrowUCN = "\u00E4\u00F6\u00FC\u00DF\u20AC\u0040";
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t> convertWindows;
std::wstring wide = convertWindows.from_bytes(narrowUCN); // Unhandled C++ exception in xlocbuf, line 426.
使用 narrowUCN = u8"\ u00E4 \ u00F6 \ u00FC \ u00DF \ u20AC \ u0040"
有效,所以我认为执行字符集有问题吗?
Using narrowUCN = u8"\u00E4\u00F6\u00FC\u00DF\u20AC\u0040"
works so I assume a problem with the execution character set?
推荐答案
Since Visual Studio 2015 Update 2, it is possible to set the execution character set to UTF-8 using the compiler option /utf-8
. Then the conversion of narrow string literals, that don't use u8
, will work. This is because those string literals are then converted to UTF-8 instead of the system's codepage (which is the default behaviour of Visual C++ compiler).
选项/utf-8
是/source-charset:utf-8
和/execution-charset:utf-8
.从上面的链接:
The option /utf-8
is a synonym for /source-charset:utf-8
and /execution-charset:utf-8
. From the link above:
在那些已经存在无BOM的UTF-8文件或更改为BOM的问题的情况下,请使用/source-charset:utf-8选项正确读取这些文件.
In those cases where BOM-less UTF-8 files already exist or where changing to a BOM is a problem, use the /source-charset:utf-8 option to correctly read these files.
在Linux和Windows之间定向代码时,使用/execution-charset或/utf-8可以有所帮助,因为Linux通常使用无BOM的UTF-8文件和UTF-8执行字符集.
Use of /execution-charset or /utf-8 can help when targeting code between Linux and Windows as Linux commonly uses BOM-less UTF-8 files and a UTF-8 execution character set.
PS:请不要将其与通用项目配置页面中的字符集设置(仅用于设置Macros Unicode/MBCS(历史原因)"相混淆.
PS: Don't confuse this with the character set setting in the common project configuration page which only sets the Macros Unicode/MBCS (historical reasons).
这篇关于设置Visual C ++编译器的执行字符集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!