News for hackers. Stuff that matters.
Re:My favorite C compiler flags during development by mytailorisrich on Sunday April 30, 2023 @04:23AM (#35760639)
It's good practice to turn on all warnings but then what? If the aim is no warnings, and that can even be a hard rule, then you also want to add -Werror to treat all warnings as errors.

That might sound drastic but that in fact enforces discipline and the removal of warnings and is IMHO very good practice especially if you start doing that from day one.

For legacy projects this can be a goal after an comprehensive phase of warnings removal.

I've worked of projects with and without "all warnings as errors" and ultimately it is well worth it.

Re:My favorite C compiler flags during development by rwmj on Sunday April 30, 2023 @04:51AM (#35760791)
Yes, but don't turn it on except in developer builds. For downstream packagers it's a pain if a newer compiler adds a warning which then breaks the build.
Re:My favorite C compiler flags during development by DyslexicAtheist on Sunday April 30, 2023 @05:02AM (#35760845)
I see your point, and agree it might be painful.

it depends on the promises the maintainer makes to themselves and to their users. I'd rather break the build and get immediately alerted by (hopefully moderately) upset downstream packagers than having this warning become visible in my users builds.

It isn't just about getting rid of a warning, but the new compiler has changed some important logic so this warning might highlight potentially undefined behavior. Am I, the maintainer OK with that - that is the main question I believe.

Might be useful to do regression with any new compiler being released especially when a product is used in safety or security critical systems.

Re:My favorite C compiler flags during development by rwmj on Sunday April 30, 2023 @05:12AM (#35760901)
The trouble is a downstream packager might be compiling the whole distro -- 1000s of packages -- and they're not in a position to fix a new warning somewhere deep inside a program they don't know and probably don't use. They are more likely to hack the build system to add -Wno-error while cursing the upstream project.
Re:My favorite C compiler flags during development by DyslexicAtheist on Sunday April 30, 2023 @05:21AM (#35760949)
fair point
Re:My favorite C compiler flags during development by tmtvl on Sunday April 30, 2023 @05:49AM (#35761072)
This may be a controversial statement, but I don't think projects should distribute any flags aside from the bare minimum to get everything working. E.g.:

  -std=c17 -lm
And let downstream add their own flags (-Owhatever, -fsomething,...) on top of that.
Re:My favorite C compiler flags during development by rwmj on Sunday April 30, 2023 @06:28AM (#35761247)
That's right. Fedora overrides a lot of flags globally, eg. adding -D_FORTIFY_SOURCE=3 and a bunch of other hardening stuff.
Re:My favorite C compiler flags during development by injinj on Sunday April 30, 2023 @06:56AM (#35761406)
That's new. Wow, latest has -mno-omit-leaf-frame-pointer (rpm --eval '%{optflags}')

Do you know of a way to get rpmbuild to keep the -ffat-lto-objects in the ar libs?

Re:My favorite C compiler flags during development by account42 on Thursday May 04, 2023 @08:09AM (#35814084)
There are many more flags such as -ffast-math where the project will be better positioned to decide if they make sense than some random packager juggling hundreds of packages. Even for optimzation flags that don't change behavior there is no clear best set of flags that you should blindly apply to all projects.
Re:My favorite C compiler flags during development by DyslexicAtheist on Sunday April 30, 2023 @04:51AM (#35760793)
not sure why your comment is downvoted.

The only way to ensure discipline and rid a large code base of warnings is to use -Werror

It may sound drastic until you're the poor intern that needs to refactor an active codebase with thousands of warning. Other than the technical debt it creates you're setting yourself up for having serious issue slipping through in all the noise.

"But some warnings might be false positives" ?

If one really must override the behavior it should be done explicitly and with a strong justification. This is possible either with a pragma[1]

  #pragma GCC diagnostic ignored "-Wfoo"
or by globally[2] removing the entire warning category.

  -Wno-foo

[1] http://gcc.gnu.org/onlinedocs/gcc/Diagnostic-Pragmas.html

[2] https://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Warning-Options...

Re:My favorite C compiler flags during development by hulitu on Sunday April 30, 2023 @05:51AM (#35761080)
> The only way to ensure discipline and rid a large code base of warnings is to use -Werror

And this works as long as everybody has the same setup as you. This is unfortunately never the case. Kernel changes, libc changes, the compiler changes and sooner or later you will break the build.

Re:My favorite C compiler flags during development by pjmlp on Sunday April 30, 2023 @06:40AM (#35761318)
Allow developers to do whatever they feel like, impose -Werror on CI/CD and PR.
Re:My favorite C compiler flags during development by flohofwoe on Sunday April 30, 2023 @05:21AM (#35760952)
Exactly, turning on -Werror is good practice during development and when running CI, but not when building the source distribution of your code (because the user might have a slightly different version of the compiler which has different warnings).
Re:My favorite C compiler flags during development by mytailorisrich on Sunday April 30, 2023 @05:33AM (#35761009)
What source distribution?

Many replies seem to imply open source by default. Of course projects may have specific requirements, and open source is indeed a specific use case, but the majority of software is commercial, closed source.

Regarding compiler versions, many projects I worked on actually check that during the build because the code may only have been designed for or tested against a specific compiler/compiler version. In general upgrading the version of the compiler is an activity in itself because, indeed, this may require solving new warnings/compilation issues and a full test campaign.

Re:My favorite C compiler flags during development by flohofwoe on Sunday April 30, 2023 @05:35AM (#35761018)
> but the majority of software is commercial, closed source

That may be true for 'end user applications', but definitely not for libraries (thankfully). I use exactly zero closed source (precompiled) dependencies in my hobby and professional work because they are such pain to work with. Thankfully these days there's an open source alternative for pretty much everything, 15 years ago the situation was much more dire.

Re:My favorite C compiler flags during development by mytailorisrich on Sunday April 30, 2023 @05:38AM (#35761031)
Oh you use plenty of closed source software in your daily life. The majority of software out there is closed source.
Re:My favorite C compiler flags during development by flohofwoe on Sunday April 30, 2023 @05:44AM (#35761053)
I edited my reply during your reply to clarify.

But when I look at the tools and applications I use daily, there's only two important applications that are closed source: VSCode and Chrome (and even this is debatable because both of those tools are just thin wrappers around Chromium: https://chromium.googlesource.com/chromium/src.git).

Re:My favorite C compiler flags during development by staunton on Sunday April 30, 2023 @06:59AM (#35761423)
Indeed, I've had good experiences using Chromium and VSCodium instead, which simply don't include the closed source parts.
Re:My favorite C compiler flags during development by pjmlp on Sunday April 30, 2023 @11:23AM (#35763400)
They are quite common in libraries for gaming industry and enterprise components.
Re:My favorite C compiler flags during development by JoshTriplett on Sunday April 30, 2023 @08:51AM (#35762080)
I personally like turning on `-Werror=specific-warning` `-Werror=another-specific-warning`, for a long list of warnings. Then, if the compiler adds a new warning, you have a chance to review it and eliminate all instances of it before making it an error.

That doesn't prevent the compiler from expanding an existing warning, but that seems to break code less often than a blanket `-Werror`.

Re:My favorite C compiler flags during development by account42 on Thursday May 04, 2023 @08:01AM (#35814035)
> If the aim is no warnings, and that can even be a hard rule, then you also want to add -Werror to treat all warnings as errors

Hard disagree. No warnings means CI should fail (or otherwise scream loudly), not every build of your in-progress working tree. You can add a commit hook if you want but if it needs to be enforced then it isn't really discipline.

Even for CI you probably don't want -Werror but instead run the build completely so that you can track all warnings.

And for source released to others -Werror is downright rude as you have no idea what warnings their possible future compiler will have.