views:

1488

answers:

7

I just read this post about why new-line warnings exist, but to be honest my team has people working on several different platforms and with several different editors (everyone uses what bests suites them), so the warning has become ubiquitous, and since its not really a warning worth taking care of it's become Noise and makes finding serious warnings a hassle.

Many times important warnings have gone unnoticed because, people got used to having a gazillion useless warnings pass by, so they obviously just stop looking at them carefully, and with reason IMHO. One could say in our case GCC is crying wolf too much for anyone to take it seriously anymore, which is a bad attitude but its just human nature.

Right now we compile with -Wall, because we want warnings, but is there a counter flag to avoid the new-line warnings ?

Thanks in advance.

Note: I Looked through the manual a bit but didn't find the answer in any place obvious so I gave up.

Note: In response to Robert Gamble's totally reasonable solution, our code is cross-platform and we have people and builds on Linux, Solaris and Windows, so the new-line... is not under consensus. And Somebody's compiler is always going to cry-wolf. Because there are over 40 developers, and other non programmer staff as well.

+3  A: 

Why don't you just make sure your files have a terminating newline like they are supposed to? This should be a simple configuration change in the offending editors and seems like a pretty easy way to "silence" the warning.

Robert Gamble
Well one reason is the code is cross-platform and we have people and builds on Linux, Solaris, Windows and Macs, so the new-line... is not under consensus. And Somebody's compiler is always going to cry-wolf.
Robert Gould
gcc won't complain if the files end with just a linefeed when run on Windows or a carriage return + linefeed when run on Unix so this should work as long as each file ends with some kind of newline.
Robert Gamble
+1 for a good answer. But we use 3 compilers... not just GCC. The other two are nice and shut up about the warning, and 90% of the team uses those compilers, just 10% of us use GCC, so the error keeps coming back for us. Ah, it'd be really nice to have such a flag...
Robert Gould
Do the other developers understand that they are generating code that isn't Standards compliant and the potential issues that surround not having an ending newline?
Robert Gamble
You always could just grep -v 'warning: no newline at end of file' if you really wanted to.
Robert Gamble
Yes we grep our automated builds, but not our local compiles, guess that could be done. As for the fact that our colleagues code is non-standard, they know it, but since in reality every compiler on earth gracefully manages the problem (with warnings perhaps) the truth is there is no consequence
Robert Gould
A: 

I'm 90% sure there is no arguemnt to turn this off.

The reason for the warning is that files without an endline give undefined behavior when compiled:

See standard: http://c0x.coding-guidelines.com/5.1.1.2.html

Here's a blog post with some python code (that I have not tried) which says it will fixup source files with this issue.

http://www.johndcook.com/blog/tag/gcc/

Donblas
In other compilers it can be turned off, I mean you can turn off all warnings if you wanted to, which is "unreasonable" of course, but possible. So there is plenty of argument for switching a useless and in real-life well defined behavior (compiles perfectly fine) warning.
Robert Gould
Ok, to be honest you are right that there is **no reason to** turn it off in the ideal programmer world, but our project suffers from some logistic problems that make a good case for turning it off.
Robert Gould
+2  A: 

There isn't one as far as i know, i've used GCC for years.

John T
thanks for confirming my fears :)
Robert Gould
Although its not the answer I wished to hear :) This is directly the correct answer to my question.
Robert Gould
A: 

Can someone enlight me? I'm using GCC 4.3.2, and compile with -Wall -pedantic -std=c89 and whatnot, but it keeps not outputting a warning about a missing newline, althought the file does not contain a newline at the end. I remember having read that warning in the past too.

FWIW, i too haven't found anything in the manpage of GCC.

[js@HOST2 cpp]$ cat file.c
#include "haha.h"
int main(void)
{

        return 0;
}[js@HOST2 cpp]$ cat haha.h
struct haha {
    int a;
};[js@HOST2 cpp]$ gcc -Wall file.c
[js@HOST2 cpp]$ gcc -Wall -pedantic -std=c89 -ansi file.c
[js@HOST2 cpp]$

What's going on?

Johannes Schaub - litb
A: 

See this SO question for more answers:

http://stackoverflow.com/questions/72271/no-newline-at-end-of-file-compiler-warning

Donblas
I originally linked that answer in my post, and read and understood it before asking, however it just gives reasons, which are valid and real, but don't solve the problem. They just explain the problem.
Robert Gould
+2  A: 

Assuming you use some kind of source control system, you could add a pre-commit hook that ensures that text files end with a proper newline. Furthermore, depending on which source control system you use, you could add a pre-commit hook that actually fixes the line ending if it's not present.

Greg Hewgill
+1. Actually this is a good solution, and will probably end up doing this. Although its kind of a roundabout way to doing this :)
Robert Gould
+1 You beat me to it.
Mr Fooz
Damn, I was just going to write this :-) We do something similar to remove tabs.
Enno
A: 

Add a hook to your source control that won't allow successful code check-in until the newline is added?

HUAGHAGUAH
The idea to add a hook that fixes the files is better, than the idea to refute the commit. I for one, maybe rare in our profession, think human needs go before the needs of the machines and compilers.
Robert Gould

related questions