是否有可能让GCC用BOM源文件编译UTF-8? [英] Is it possible to get GCC to compile UTF-8 with BOM source files?

查看:795
本文介绍了是否有可能让GCC用BOM源文件编译UTF-8?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Visual Studio中,我可以使用unicode符号,如π

/ strong>和²。 Visual Studio总是将源文件保存为带有BOM(字节顺序标记)的UTF-8。



例如:

  // A =π.r²
doubleπ= 3.14;

仅当我先删除BOM时,GCC才会愉快地编译这些文件。如果我不删除物料清单,我会收到类似这样的错误:


wwga_hydutils.cpp:28:9:error:stray'\ 317'in program



wwga_hydutils.cpp:28:9:error:stray'\200'in program


这引出了我的疑问:

有没有办法让GCC编译UTF-8文件而不先删除BOM ?






我正在使用:


  • Windows 7

  • Visual Studio 2010



和:




  • uBuntu Oneiric 11.10

  • GCC 4.6.1(由 apt-get install gcc






编辑:



作为第一位评论者指出,我的问题是不是 BOM,但是在字符串常量之外有非ascii字符。 GCC不喜欢符号名称中的非ascii字符,但事实证明GCC完全兼容带有BOM的UTF-8。

解决方案

<根据 GCC Wiki ,这还不支持。您可以使用 -fextended-identifiers 并预处理您的代码以将标识符转换为UCN。从链接页面:

  perl -pe'BEGIN {binmode STDIN,:utf8; } s /(.)/ ord($ 1)< 128? $ 1:sprintf(\\U%08x,ord($ 1))/ ge;'

另请参阅 g ++ unicode变量名称I develop C++ cross platform using Microsoft Visual Studio on Windows and GCC on uBuntu Linux.

In Visual Studio I can use unicode symbols like "π" and "²" in my code. Visual Studio always saves the source files as UTF-8 with BOM (Byte Order Mark).

For example:

// A = π.r²
double π = 3.14;

GCC happily compiles these files only if I remove the BOM first. If I do not remove the BOM, I get errors like these:

wwga_hydutils.cpp:28:9: error: stray ‘\317’ in program

wwga_hydutils.cpp:28:9: error: stray ‘\200’ in program

Which brings me to the question:

Is there a way to get GCC to compile UTF-8 files without first removing the BOM?


I'm using:

  • Windows 7
  • Visual Studio 2010

and:

  • uBuntu Oneiric 11.10
  • GCC 4.6.1 (as provided by apt-get install gcc)

Edit:

As the first commenter pointed out, my problem was not the BOM, but having non-ascii characters outside of string constants. GCC does not like non-ascii characters in symbol names, but it turns out GCC is fully compatible with UTF-8 with BOM.

解决方案

According to the GCC Wiki, this isn't supported yet. You can use -fextended-identifiers and pre-process your code to convert the identifiers to UCN. From the linked page:

perl -pe 'BEGIN { binmode STDIN, ":utf8"; } s/(.)/ord($1) < 128 ? $1 : sprintf("\\U%08x", ord($1))/ge;' 

See also g++ unicode variable name and Unicode Identifiers and Source Code in C++11?

这篇关于是否有可能让GCC用BOM源文件编译UTF-8?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆