Warning: no newline at end of file
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?
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.
The compiler doesn't care, but it warns you about it, since it may break something else.
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.
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.
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
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".
"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".
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."
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."
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.
#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.
@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.
#include "fileone.h"#include "filetwo.H"
Which wouldn't compile regardless of whether or not there was a new line in the headers.
Not according to a strict reading of the C++ standard:
The new-line is considered part of the directive and is replaced.
Σnigma
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
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement