欢迎使用CSDN-markdown编辑器

来源:互联网 发布:中国外交有多阴险知乎 编辑:程序博客网 时间:2024/06/06 08:22

VC 下的 utf-8,汉字编码导致编译出错

VC 对 utf-8 的支持情况

—— Build started: Project: UTF8, Configuration: Debug Win32 ——
1> main.cpp
1>d:\work\toy\utf8\utf8\main.cpp : warning C4819: The file contains a character that cannot be represented in the current code page (949). Save the file in Unicode format to prevent data loss
1>d:\work\toy\utf8\utf8\main.cpp(1): error C2001: newline in constant
1>d:\work\toy\utf8\utf8\main.cpp(2): fatal error C1004: unexpected end-of-file found
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
I want to note that it is very difficult to switch our codebase into UTF-16 or UTF-8 with BOM as tools used in our tool chain do not properly support these encoding.s

since UTF-8 without BOM is actually de facto in many project (especially outside Microsoft ecosystem), I think that it is essential to support UTF-8 without BOM in MSVC. I don’t think that it is technically infeasible, as VS text editor has already auto-detected UTF-8 without BOM for a while.

If it is really impossible due to backward compatibility, then at least MSVC should provide a workaround such as a compiler option to enforce a specific encoding scheme over a project, rather than an encoding guessed from a system locale.

微软的回答是
Hi summerlight
Unfortunately, we currently have no plans to implement the support of UTF-8 files without byte order marks.

Karl Niu
Visual C++ Team

但是 GCC 喜欢 UTF-8 编码的源文件, 如果因为汉字的编码问题导致编译出错,在字符串的末尾添加一个空格看看,或许能解决问题。

0 0
原创粉丝点击