C++ “文件末尾没有换行符”编译器警告
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/72271/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"No newline at end of file" compiler warning
提问by TJ Seabrooks
What is the reason for the following warning in some C++ compilers?
在某些 C++ 编译器中出现以下警告的原因是什么?
No newline at end of file
文件末尾没有换行符
Why should I have an empty line at the end of a source/header file?
为什么我应该在源/头文件的末尾有一个空行?
回答by TJ Seabrooks
Think of some of the problems that can occur if there is no newline. According to the ANSI standard the #include
of a file at the beginning inserts the file exactly as it is to the front of the file and does not insert the new line after the #include <foo.h>
after the contents of the file. So if you include a file with no newline at the end to the parser it will be viewed as if the last line of foo.h
is on the same line as the first line of foo.cpp
. What if the last line of foo.h was a comment without a new line? Now the first line of foo.cpp
is commented out. These are just a couple of examples of the types of problems that can creep up.
想想如果没有换行可能会出现的一些问题。根据 ANSI 标准#include
,文件开头将文件完全按原样插入文件的前面,并且不会#include <foo.h>
在文件内容之后插入新行。因此,如果在解析器的末尾包含一个没有换行符的文件,它将被视为最后foo.h
一行与foo.cpp
. 如果 foo.h 的最后一行是没有换行的注释怎么办?现在第一行foo.cpp
被注释掉了。这些只是可能出现的问题类型的几个例子。
Just wanted to point any interested parties to James' answer below. While the above answer is still correct for C, the new C++ standard (C++11) has been changed so that this warning should no longer be issued if using C++ and a compiler conforming to C++11.
只是想向任何感兴趣的各方指出下面詹姆斯的回答。虽然上述答案对 C 仍然正确,但新的 C++ 标准 (C++11) 已更改,因此如果使用 C++ 和符合 C++11 的编译器,则不应再发出此警告。
From C++11 standard via James' post:
从 C++11 标准通过 James 的帖子:
A source file that is not empty and that does not end in a new-line character, or that ends in a new-line character immediately preceded by a backslash character before any such splicing takes place, shall be processed as if an additional new-line character were appended to the file (C++11 §2.2/1).
非空且不以换行符结尾的源文件,或在任何此类拼接发生之前以紧跟反斜杠字符开头的换行符结尾的源文件,应被处理,就好像一个额外的新-行字符被附加到文件 (C++11 §2.2/1)。
回答by James McNellis
The requirement that every source file end with a non-escaped newline was removed in C++11. The specification now reads:
在 C++11 中删除了每个源文件以非转义换行符结尾的要求。规范现在写着:
A source file that is not empty and that does not end in a new-line character, or that ends in a new-line character immediately preceded by a backslash character before any such splicing takes place, shall be processed as if an additional new-line character were appended to the file (C++11 §2.2/1).
非空且不以换行符结尾的源文件,或在任何此类拼接发生之前以紧跟反斜杠字符开头的换行符结尾的源文件,应被处理,就好像一个额外的新-行字符被附加到文件 (C++11 §2.2/1)。
A conforming compiler should no longer issue this warning (at least not when compiling in C++11 mode, if the compiler has modes for different revisions of the language specification).
符合标准的编译器不应再发出此警告(至少在 C++11 模式下编译时不会发出此警告,如果编译器具有针对不同语言规范修订版的模式)。
回答by Igor Semenov
C++03 Standard [2.1.1.2] declares:
C++03 标准 [2.1.1.2] 声明:
... If a source file that is not empty does not end in a new-line character, or ends in a new-line character immediately preceded by a backslash character before any such splicing takes place, the behavior is undefined.
... 如果非空的源文件不以换行符结尾,或者在任何此类拼接发生之前以换行符结尾,且紧跟反斜杠字符,则行为未定义。
回答by Vytautas Shaltenis
The answer for the "obedient" is "because the C++03 Standard says the behavior of a program not ending in newline is undefined" (paraphrased).
“顺从”的答案是“因为 C++03 标准说不以换行符结尾的程序的行为是未定义的”(释义)。
The answer for the curious is here: http://gcc.gnu.org/ml/gcc/2001-07/msg01120.html.
好奇的答案在这里:http: //gcc.gnu.org/ml/gcc/2001-07/msg01120.html。
回答by Leigh Caldwell
It isn't referring to a blank line, it's whether the last line (which can have content in it) is terminated with a newline.
它不是指空行,而是最后一行(其中可以包含内容)是否以换行符结尾。
Most text editors will put a newline at the end of the last line of a file, so if the last line doesn't have one, there is a risk that the file has been truncated. However, there are valid reasons why you might not want the newline so it is only a warning, not an error.
大多数文本编辑器会在文件最后一行的末尾放置一个换行符,因此如果最后一行没有换行符,则存在文件被截断的风险。但是,您可能不想要换行符是有正当理由的,因此它只是警告,而不是错误。
回答by moonshadow
#include
will replace its line with the literal contents of the file. If the file does not end with a newline, the line containing the #include
that pulled it in will merge with the next line.
#include
将用文件的文字内容替换它的行。如果文件不以换行符结尾,包含#include
它的行将与下一行合并。
回答by divesh
I am using c-free IDE version 5.0,in my progrm either of 'c++' or 'c' language i was getting same problem.Just at the end of the programi.e. last line of the program(after braces of function it may be main or any function),press enter-line no. will be increased by 1.then execute the same program,it will run without error.
我使用的是无 c 的 IDE 5.0 版,在我的“c++”或“c”语言程序中,我遇到了同样的问题。就在程序的末尾,即程序的最后一行(在函数大括号之后,它可能是main 或任何函数),按 Enter-line no。将增加1.然后执行相同的程序,它会运行没有错误。
回答by Jan-Philip Loos
Of course in practice every compiler adds a new line after the #include. Thankfully. – @mxcl
当然,实际上每个编译器都会在#include 之后添加一个新行。谢天谢地。– @mxcl
not specific C/C++ but a C dialect: when using the GL_ARB_shading_language_include
extension the glsl compiler on OS X warns you NOTabout a missing newline. So you can write a MyHeader.h
file with a header guard which ends with #endif // __MY_HEADER_H__
and you willlose the line after the #include "MyHeader.h"
for sure.
不是特定的 C/C++ 而是 C 方言:使用GL_ARB_shading_language_include
扩展时,OS X 上的 glsl 编译器会警告您不要丢失换行符。因此,您可以编写一个MyHeader.h
带有标题保护的文件,该文件以 结尾,#endif // __MY_HEADER_H__
并且您肯定会丢失该行#include "MyHeader.h"
。
回答by skyking
Because the behavior differs between C/C++ versions if file does not end with new-line. Especially nasty is older C++-versions, fx in C++ 03 the standard says (translation phases):
因为如果文件不以换行符结尾,C/C++ 版本之间的行为会有所不同。尤其令人讨厌的是旧的 C++ 版本,C++ 03 中的 fx 标准说(翻译阶段):
If a source file that is not empty does not end in a new-line character, or ends in a new-line character immediately preceded by a backslash character, the behavior is undefined.
如果非空的源文件不以换行符结尾,或以换行符结尾,后面紧跟反斜杠字符,则行为未定义。
Undefined behavior is bad: a standard conforming compiler could do more or less what it wants here (insert malicous code or whatever) - clearly a reason for warning.
未定义的行为是不好的:符合标准的编译器可以或多或少地在这里做它想做的事情(插入恶意代码或其他什么)——这显然是警告的原因。
While the situation is better in C++11 it is a good idea to avoid situations where the behavior is undefined in earlier versions. The C++03 specification is worse than C99 which outright prohibits such files (behavior is then defined).
虽然这种情况在 C++11 中更好,但最好避免在早期版本中行为未定义的情况。C++03 规范比 C99 更糟糕,C99 完全禁止此类文件(然后定义行为)。
回答by mwfearnley
This warning might also help to indicate that a file could have been truncated somehow. It's true that the compiler will probably throw a compiler error anyway - especially if it's in the middle of a function - or perhaps a linker error, but these could be more cryptic, and aren't guaranteed to occur.
此警告也可能有助于表明文件可能以某种方式被截断。确实,无论如何编译器都可能会抛出编译器错误——尤其是如果它在函数中间——或者可能是链接器错误,但这些可能更神秘,并且不能保证会发生。
Of course this warning also isn't guaranteed if the file is truncated immediately after a newline, but it could still catch some cases that other errors might miss, and gives a stronger hint to the problem.
当然,如果在换行后立即截断文件,也不能保证此警告,但它仍然可以捕获其他错误可能遗漏的某些情况,并为问题提供更强的提示。