“No newline at end of file” compiler warning

后端 未结 11 945
逝去的感伤
逝去的感伤 2020-11-27 09:50

What is the reason for the following warning in some C++ compilers?

No newline at end of file

Why should I have an empty line at

相关标签:
11条回答
  • 2020-11-27 10:22

    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.


    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.

    From C++11 standard via James' post:

    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).

    0 讨论(0)
  • 2020-11-27 10:23

    The requirement that every source file end with a non-escaped newline was removed in C++11. The specification now reads:

    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).

    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).

    0 讨论(0)
  • 2020-11-27 10:27

    That's not an error. It's just a warning.

    Open the file in an editor, go to the last line of the file, and hit enter to add a blank line to the end of the file.

    Though, besides that, you should be using #include <iostream> instead of <iostream.h>. Then put in a using std::cout; after it.

    0 讨论(0)
  • 2020-11-27 10:29

    The answer for the "obedient" is "because the C++03 Standard says the behavior of a program not ending in newline is undefined" (paraphrased).

    The answer for the curious is here: http://gcc.gnu.org/ml/gcc/2001-07/msg01120.html.

    0 讨论(0)
  • 2020-11-27 10:35

    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.

    0 讨论(0)
  • 2020-11-27 10:36

    Of course in practice every compiler adds a new line after the #include. Thankfully. – @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 NOT about a missing newline. So you can write a MyHeader.h file with a header guard which ends with #endif // __MY_HEADER_H__ and you will lose the line after the #include "MyHeader.h" for sure.

    0 讨论(0)
提交回复
热议问题