Programming

(C++) “No newline at end of file” compiler warning

steloflute 2013. 2. 7. 23:29

http://stackoverflow.com/questions/72271/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 " 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.

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

 

 

'Programming' 카테고리의 다른 글

(Java) How to read/write a serial port with Java on Windows? [closed]  (0) 2013.02.09
RUDY Slowloris  (1) 2013.02.08
(git) Visual Studio .gitignore file  (0) 2013.02.05
Python IDLE Themes  (0) 2013.02.04
Github: README and README.md  (0) 2013.02.03