星期一, 11月 12, 2007

DOS 與 Unix 換行符號的問題

編譯時有時會出現
uncgi.c:52: parse error before `>='
uncgi.c: In function `url_unescape':
uncgi.c:104: stray '\' in program
uncgi.c:104: parse error before `('
uncgi.c:104: parse error before `||'
uncgi.c:104: stray '\' in program
uncgi.c:109: parse error before `else'


而 code 裡看起來感覺卻很正常
#define ishex(x) (((x) >= '0' && (x) <= '9') || ((x) >= 'a' && (x) <= 'f') || \
((x) >= 'A' && (x) <= 'F'))


但如果將句尾的 \ 移除,此部份即可正常編譯

此時可以試試用 flip -ub *.c 或 flip -ub *.h 將換行符號轉為 unix 型式,再編輯看看。
若這樣就可以正常編譯,那麼問題就可以確定是 DOS 及 Unix 所引起的了。
可以參考下面這篇的解釋
http://sources.redhat.com/ml/ecos-discuss/2001-11/msg00064.html
原文節錄如下
li hui writes:
> I meet a strange problem, when I use '\' in my program
> to indicate a newline, compiler(i386-elf-gcc under
> CYGWIN) promote error that stray '\' in program.
> I was puzzled by this error.

Chances are this is a newlines problem. Your sources are mounted in text
mode rather than binary mode, or vice versa; I don't understand nasty
windows-isms myself, but I know that can be the source of that problem.

The underlying cause is that you wrote (for example)

#define FRED(x) \
x++

and you want the compiler to see (as a series of bytes)

......[NL]#define FRED(x) \[NL][TAB]x++[NL]......

(newline then tab between "\" and "x") but it is actually seeing

....[CR][NL]#define FRED(x) \[CR][NL][TAB]x++[CR][NL]......

because the file is being presented with DOS line endings (CR-LF) rather
than just an LF. And \[CR] is not a continuation symbol to the compiler.

I suspect a more detailed answer will be available in the archives of this
list.

HTH,

- Huge




沒有留言: