在Visual Studio自动生成的项目中,遇见了一件关于文件编码的问题,集中在相似于如下的语句上:html
DASLog (DASProtWarn, L"(%s)消息超时,进入慢循环召唤模式。", GetHierarchyName());
编译时会出现如下错误:this
error C2001:常量中有换行符
该错误的缘由很显然是文件编码的问题,在网上搜索了一下,找到了以下解决办法:编码
(1)所有用英文编码,不要用中文spa
(2)偶数中文 或 结尾加英文的符号,如"."设计
(3)将文件编码进行一个手动(如记事本)转换,改为UTF-8格式code
我采用了第二种方式,直接将末尾的中文“。”改为了英文的“.”,该错误就解决了!htm
至于该问题的详细缘由,参考一位网友的分析说明(引用:http://www.cnblogs.com/cocos2d-x/archive/2012/02/26/2368873.html):blog
有个叫wva的人遇到过相似问题,他向微软提交了此bug http://connect.microsoft.com/VisualStudio/feedback/details/341454/compile-error-with-source-file-containing-utf8-strings-in-cjk-system-locale 根据Visual C++ Compiler Team员工的解释: The compiler when faced with a source file that does not have a BOM the compiler reads ahead a certain distance into the file to see if it can detect any Unicode characters - it specifically looks for UTF-16 and UTF-16BE - if it doesn't find either then it assumes that it has MBCS. I suspect that in this case that in this case it falls back to MBCS and this is what is causing the problem. 看见了吧,对于那些没有BOM的文件设计就是这样的。从语气上看,他们编译器小组也不打算修改设计。因此呢,在VC上使用“无签名的UTF-8”编码的文件,你就是在抱着一颗不定时炸弹玩耍。由于你永远都不敢肯定哪些词能经过编译,哪些不能! 若是要硬编码字符串,即使是字符编码转换也不必定能帮不上你。一旦你为此增长了字符编码转换的代码,那么也意味着可移植性下降了。由于这从根本上是编译器决定的。