Wednesday, June 9, 2010

warning: no new line at end of file

For the longest time, I would run in to this when compiling files with gcc. I never knew why. I always surmised that it must be something to do with the way older compilers worked, in that they probably concatenated a whole bunch of C files and read them from their standard input. Doing so has the advantage that your compiler doesn't have to do anything special for handling multiple files. all it wants, it get from one interface: it's standard input. That would also allow you to potentially use it as a sink in a pipe chain command.

E.g.


cat foo.c bar.c sna.c fu.c | ccompiler


if any of these files were not ending on a new line, you'd run in to possible artifacts

E.g. if foo.c was

#ifdef _FOO_
exter int foo;
#endif


and bar.c was

#ifdef _BAR_
extern int bar;
#endif


cat foo.c bar.c would produce

#ifdef _FOO_
exter int foo;
#endif#ifdef _BAR_
extern int bar;
#endif


which would cause a compile error.

However, I now know that this is done because the C standard requires it!


The C language standard says
A source file that is not empty shall end in a new-line character, which shall not be immediately preceded by a backslash character.

Since this is a "shall" clause, we must emit a diagnostic message for a violation of this rule.

This is in section 2.1.1.2 of the ANSI C 1989 standard. Section 5.1.1.2 of the ISO C 1999 standard (and probably also the ISO C 1990 standard).
--
Jim Wilson, GNU Tools Support, http://www.SpecifixInc.com

No comments: