gcc编译报错:程序中有游离的‘\357’‘\273’‘\277’等

造成的原因主要有两个:
1. 程序(*.c,*.h)中使用了中文的标点符号(全角),比如;,},+。
改成英文的标点半角符号就行了。
甚至有时候空格也会出现类似错误,删掉该空格 重新输入。
vim里面做类似替换还是很容易的。

如何看到报错的符号?
od -c hello.c > log.txt
在log中就能看到符号了

2.
如果替换成了英文标点还出错的话,还报此错误,那么就是文件存贮格式的问题了。
一般在windows下的文件都存成ansi格式,为了在linux下能通用,建议保存成UTF-8不带BOM
编码格式,因为目前gcc和g++不支持UTF-8带BOM编码格式。

用g++编译的时候碰到UTF-8 BOM错误怎么办?
$ g++ -I../../include unit_test.cpp -o unit_test
unit_test.cpp:1: 错误: 程序中有游离的'\357'
unit_test.cpp:1: 错误: 程序中有游离的'\273'
unit_test.cpp:1: 错误: 程序中有游离的'\277'
In file included from unit_test.cpp:63:
...

或在英文系统下:
$ g++ -I../../include unit_test.cpp -o unit_test
unit_test.cpp:1: error: stray '\357' in program
unit_test.cpp:1: error: stray '\273' in program
unit_test.cpp:1: error: stray '\277' in program
In file included from unit_test.cpp:63:
...


如何判断文件是否是使用UTF-8 BOM存储的?

执行下面的命令:
$ cat cpp/src/unit_test/unit_test.cpp |hd -n 10
00000000 ef bb bf 2f 2a 2a 2a 2a 2a 2a |.../******|
0000000a

posted on 2009-06-17 09:20  DP.  阅读(15629)  评论(0编辑  收藏  举报