"No Newline at End of File" Compiler Warning

No newline at end of file compiler warning

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

what is the meaning of warning : No new line at end of file?

The C language requires that every source file must end with a newline (from C99 5.1.1.2/1):

A source file that is not empty shall end in a new-line character, which shall not be immediately preceded by a backslash character before any such splicing takes place.

(C++ also had this requirement prior to C++11)

no newline at end of file C

Text processing programs don't always agree on whether a \n character is line separator or line terminator - the difference being whether or not the last line in a file should be followed by a \n character or not.

I've never seen GCC complain about this, but you didn't indicate where the error is actually coming from.

You should be able to correct the situation by simply pressing ENTER after the last line in your file.

Alternatively, you could open, and them save the file using an editor like Vim, which considers it a line terminator.

As a matter of comparison, most UNIX tools also consider \n a line terminator, and include it after the final line. This allows one to cat a file without corrupting whatever follows (e.g. another file, the shell prompt, etc.)

No newline at end of file warning, even after putting in a newline

It ended up that not initializing a boolean variable to false was the issue. However, I'm not sure whether it was actually the initialization that was the problem, or just the fact that the text editor might not actually save a file if you just append whitespace to the end of it. For those wondering, this was happening to me in Notepad++.

Why GCC doesn't generate any warnings about newline at end of file?

There's a problem, newlines are not well standard defined, as different systems have different new line conventions. But you are right... if the standard says a compiler must emit a warning in that case and gcc doesn't, it should be filed as a noncomplianance issue.

But I agree with @supercat's answer, in the sense that it can be assumed that a file without a final \n can be safely interpreted as a properly delimited text file with no line ending at the end... as the \n can be interpreted as a line separator character, and not a line ending one. In case this interpretation is valid, an empty file would be parsed as a one empty line file with no problem for the compiler to parse it, and no warning should be issued in that case. The same applies to any file without a final \n, and a file finished with a \n should be interpreted as a n + 1 lines file, with an extra empty line (this doesn't make any difference to the meaning of the C code inside, I'm afraid)

This will be probably the response you'll get in case you go to the gcc project to complain, so be prudent, but don't hesitate and do it.

By the way, have you tried to feed the compiler with a final \\ character (without a \nchar) the compiler is allowed to insert the final newline to simulate a properly defined file, but the preprocessor has to deal in a special form in case a \\ character is followed by a new line. In that case, the compiler should emit something, as you cannot continue past the last line of the file. Clang doesn't say anything in case the last line terminates in a \\ (that is a nonconformance) let's see what does gcc.... (sorry, I have no access to a gcc by now)

What flag silences GCC's warning about no newline at file-endings?

There isn't one as far as i know, i've used GCC for years.

Update:
There should not be any warnings raised with C++11 standard. Related Q

no newline at end of file error in Xcode 4.3 beta 2

You can disable this in the Project Settings > Build Settings, under the "Apple LLVM compiler 3.1 warnings" set "Missing Newline At End Of File" to "No".

C++ Standards (newline ending of source files)

The given code is legal in the case of C++, but not for C.

Indeed, the C (N1570) standard says:

Each instance of a backslash character (\) immediately followed by a new-line
character is deleted, splicing physical source lines to form logical source lines.
Only the last backslash on any physical source line shall be eligible for being part
of such a splice. A source file that is not empty shall end in a new-line character,
which shall not be immediately preceded by a backslash character before any such
splicing takes place.

The C++ standard (N3797) formulates it a bit differently (emphasis mine):

Each instance of a backslash character (\) immediately followed by a new-line character is deleted,
splicing physical source lines to form logical source lines. Only the last backslash on any physical
source line shall be eligible for being part of such a splice. If, as a result, a character sequence that
matches the syntax of a universal-character-name is produced, the behavior is undefined. 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.



Related Topics



Leave a reply



Submit