Sign in to follow this  
ukdeveloper

Warning: no newline at end of file

Recommended Posts

ukdeveloper    264
Just wondering why having a newline at the end of a file is so important to gcc? It's really easy to fix, as I say, but why is it a problem? Surely the compiler couldn't care less about whitespace?

Share this post


Link to post
Share on other sites
ToohrVyk    1596
UNIX text files are assumed to end in a newline, and many programs may use this. Otherwise, this can cause problems with some implementations of the usual file-manipulation programs can miss the last line when processing.

The compiler doesn't care, but it warns you about it, since it may break something else.

Share this post


Link to post
Share on other sites
NotAYakk    876
There is a text-file standard that states that all text files end in a newline.

As you don't have that newline, that indicates you are not technically a text file. Other tools that aren't as forgiving as gcc might barf, or just skip the last line of your file.

Which, let me tell you, can lead to really hard to track down bugs.

So GCC gives you a warning that some tool you own is creating invalid text files.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
It not just gcc depending on the level of warning set msc will give this warning aswell. I think its due to the fact its a check to make sure the file is not empty and contains at least one line. That's just a guess tho

Share this post


Link to post
Share on other sites
Ra    1062
link

"The ISO C standard, section 5.1.1.2, states that source files must end with
new lines."


ISO C++ standard: 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, the behavior is undefined".

Share this post


Link to post
Share on other sites
Washu    7829
And for the case of non-source files (such as headers):
ISO C++ standard: 16 - 1: "The last token in the sequence is the first new-line character that follows the first token in the sequence."

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
I think the reason is that the files will not appear to follow on from each other. Imagine two header files with

#ifdef _file_1_
#define _file_1_
#endif


and
#ifdef _file_2_
#define _file_2_
#endif

if there was no CR/LF at the end of file one , you could get


#ifdef _file_1_
#define _file_1_
#endif#ifdef _file_2_
#define _file_2_
#endif


which would be parsed incorrectly.


Share this post


Link to post
Share on other sites
Ezbez    1164
@AP - only if you were to do:

#include "fileone.h"#include "filetwo.H"

Which wouldn't compile regardless of whether or not there was a new line in the headers.

Share this post


Link to post
Share on other sites
Enigma    1410
Not according to a strict reading of the C++ standard:
Quote:
C++ Standard, Section 16.2, Paragraph 2
A preprocessing directive of the form

   # include <h-char-sequence> new-line

searches a sequence of implementation-defined places for a header identified uniquely by the specified sequence between the < and > delimiters, and causes the replacement of that directive by the entire contents of the header. How the places are specified or the header identified is implementation defined.

The new-line is considered part of the directive and is replaced.

Σnigma

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this