"No newline at end of file" compiler warning

What is the factor for the adhering to caution in some C+npls compilers?

No newline at end of documents

Why should I have a vacant line at the end of a source/header documents?

196
2022-06-25 03:05:54
Source Share
Answers: 5

C+npls 03 Standard [2.1.1 .2] proclaims:

If a resource documents that is not vacant does not end in a new - line personality, or finishes in a new - line personality quickly come before by a backslash personality prior to any kind of such splicing happens, the actions is undefined.

28
2022-06-25 03:52:27
Source

Think of several of the troubles that can take place if there is no newline. According to the ANSI criterion the #include of a documents at the starting inserts the documents specifically as it is to the front of the documents and also does not insert the new line after the #include <foo.h> after the materials of the documents. So if you include a documents without newline at the end to the parser it will certainly be considered as if the last line of foo.h gets on the very same line as the first line of foo.cpp. What happens if the last line of foo.h was a comment without a new line? Currently the first line of foo.cpp is commented out. These are simply a number of instances of the sorts of troubles that can approach.


Simply intended to aim any kind of interested events to James' solution listed below. While the above solution is still proper for C, the new C+npls typical (C+npls 11) has actually been transformed to make sure that this caution needs to no more be released if making use of C+npls and also a compiler satisfying C+npls 11.

From C+npls 11 typical using James' blog post:

A resource documents that is not vacant which does not end in a new - line personality, or that finishes in a new - line personality quickly come before by a backslash personality prior to any kind of such splicing happens, will be refined as if an added new - line personality were added to the documents (C+npls 11 cfr6.2.1cfr1).

225
2022-06-25 03:52:20
Source

It isn't describing an empty line, it is whether the last line (which can have web content in it) is ended with a newline.

The majority of full-screen editor will certainly place a newline at the end of the last line of a file, so if the last line does not have one, there is a threat that the file has actually been trimmed. Nonetheless, there stand reasons that you could not desire the newline so it is just a caution, not a mistake.

8
2022-06-25 03:24:45
Source

The solution for the "obedient" is "because the C+npls 03 Standard claims the actions of a program not finishing in newline is undefined" (paraphrased).

The solution for the interested is below: http://gcc.gnu.org/ml/gcc/2001-07/msg01120.html.

17
2022-06-25 03:12:43
Source

#include will certainly change its line with the actual materials of the documents. If the documents does not end with a newline, the line having the #include that drew it in will certainly combine with the next line.

5
2022-06-25 03:12:41
Source