“No newline at end of file” compiler warning

2019-01-05 08:25发布

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 the end of a source/header file?

11条回答
啃猪蹄的小仙女
2楼-- · 2019-01-05 08:43

C++03 Standard [2.1.1.2] declares:

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

查看更多
聊天终结者
3楼-- · 2019-01-05 08:44

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 program i.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.

查看更多
Summer. ? 凉城
4楼-- · 2019-01-05 08:45

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

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

查看更多
戒情不戒烟
5楼-- · 2019-01-05 08:45

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.

查看更多
叼着烟拽天下
6楼-- · 2019-01-05 08:46

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.

查看更多
Juvenile、少年°
7楼-- · 2019-01-05 08:53

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 C++11 conformant compiler.

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

查看更多
登录 后发表回答