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

岁酱吖の 提交于 2019-12-05 06:54:28
John T

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

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.

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.

-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.

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/

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

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