为何会产生warning: no newline at end of file?

来源:互联网 发布:淘宝店铺号码怎么查找 编辑:程序博客网 时间:2024/04/29 13:27

在linux下编译c程序,如果在文件末尾不加一个空行,那么编译过程中会提示警告:
 “warning: no newline at end of file.”
借助搜索引擎,很容易就找到了答案,比如:
 “在源程序中写到最后一行时在按一下enter建,让行尾只空出一行,重新编译试一下。”

不错,能借助搜索引擎找到答案,这点很好。
可是你有再深入一点想想为什么要这么做么?

 

应付者通常是找到答案就了事了,根本没有深入去思考。
也许对他来说,在技术上,这样应付一下就可以了。
可是当应付成了习惯,就成为了阻碍他前进的绊脚石。

现代网络环境的发展,搜索引擎索引内容量的极大提高,的确让我们节省了不少解决问题的时间。但是,过份依赖于搜索引擎,就容易丧失独立思考的能力,如此因小失大,悲哉。

就这个问题,在百度上搜索答案,找到的都是告诉你在文件尾加一空行,没有一篇是解释原因的。这也就是当前国内的学习气氛,大多数人的学习态度——浮燥、浅尝辄止。当然,这也不排除因为语言障碍造成的信息隔离,但至少,多数人没有去找过或根本没想过原因。

用google搜索了一下,翻了几篇帖子,很快就找到了答案:
http://www.linuxquestions.org/questions/showthread.php?t=281830

"It's not an extra new-line, each line should be terminated by an "end of line" tag, and this is /n under unix.
On MacOS, this used to be /r, perhaps the reason why gcc is more tolerant on newer releases of this O/S."
-- 有人思考过 --

"do you know real reason of that?"
-- 马上就有人提出质疑 --

"There's probably still some ancient compilers out there (maybe even very old versions of GCC?) that want each line to end with a newline or else they crash. My guess is that GCC is just trying to help you make your code compatible with these dino's."
-- 接着就有人提出自己的观点 --

"Actually, gcc warns about it because it has to according to the standard. Check out this link for a brief description: http://gcc.gnu.org/ml/gcc/2003-11/msg01568.html
If you have a compiler that doesn't warn you about the lack of a newline character then that compiler doesn't meet the standards. Burn the disc it came on and get a real compiler."
-- 很快,依靠大家的力量,借助网络,答案浮出水面 --

"The C language standard says
A source file that is not empty shall end in a new-line character, which shall not be immediately preceded by a backslash character.

0 0