What flag silences GCC's warning about no newline at file-endings?

∥☆過路亽.° 提交于 2019-12-22 05:28:07

问题


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?

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.


回答1:


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

Update: There should not be any warnings raised with C++11 standard. Related Q




回答2:


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.




回答3:


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.




回答4:


-Wno-eof-newline

This was added with the fix for gcc bug 14331


Oddly enough I can't actually get gcc to output a warning for missing newlines. I guess newer versions have dispensed with this warning altogether.

I can get gcc to accept -Wno-eof-newline but it complains of unrecognized flags when I try -Weof-newline. C++11 removed the requirement for newlines at the end of files but for writing portable code in the old standards it really should be possible to enable such pedantic warnings.


Fortunately clang does still correctly support diagnostics about missing newlines: This warning can be enabled with -Wnewline-eof in all modes, or in C++11 and higher modes it can also be enabled with -Wc++98-compat-pedantic.

These warnings are off by default, but if you're taking advantage of clang's -Weverything flag to enable a 'subtractive' strategy for controlling warnings, then in C++11 and higher modes you need both -Wno-newline-eof and -Wno-c++98-compat-pedantic to disable the warning.




回答5:


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/




回答6:


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



来源:https://stackoverflow.com/questions/282851/what-flag-silences-gccs-warning-about-no-newline-at-file-endings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!