7 ms·
-Werror Is not your friend (2017)
- charcircuit 3y agoThe dependency on the toolchain should be made explicit and this issue goes away. When updating the toolchain you can set default flags to disable the new warnings if you wish to fix each warning one a time.
- mort96 3y agoThat's fine if you're not writing open source code and don't plan on updating your toolchain frequently. For the rest of us? -Werror is not our friend.
- deleted 3y ago[deleted]
- charcircuit 3y agoIt works the same with open source. If your open source library is always built with clang 16 then it will never fail to compile. If people want to use an unsupported toolchain they can figure out how to get it working themselves.
- mort96 3y agoAs an author of open source software, you must expect people to use their system's toolchain. You can define minimum versions sure, but if you use Clang 16, you really ought to not break when a user tries to compile with Clang 17, unless your build system actually automatically downloads and uses the right clang version automatically. Most of us aren't using such a build system.
- charcircuit 3y ago>you must expect people to use their system's toolchain Why? I don't expect people to build against their system libraries. >unless your build system actually automatically downloads and uses the right clang version automatically Nix, bazel, buck, etc all support defining the toolchain for your build. Reproducable builds are useful. You are supporting a known configuration. Crashes can all be collected an easily analyzed. You can be sure you have a properly compiled version by verifying the hash. >Most of us aren't using such a build system. That's okay. You can just ingest a prebuilt artifact if you aren't capable of tying it into your build system.
- aflag 3y ago> Why? Because that's what people do. One of the big reasons that build open source software is to have fun and work together with other hobbyists, some of each don't even write software professionally at all. If you want to have fun with open source you really want to keep things simple and keep the entry barrier low. Otherwise, it'll just feel like an unpaid job.
- charcircuit 3y agoSome people want to develop open source software in a professional manner. Having to support the output of a single toolchain is the definition of keeping things simple.
- mort96 3y agoTo be honest I don't understand what your goal is. Use one toolchain to produce the binaries you distribute, sure. Only provide support for those official binaries or binaries compiled with the officially supported toolchain, why not. And use -Werror with that standardized toolchain in CI to ensure builds with warnings don't get committed, I think that's great. But why are you hell bent on making it harder for your users to build from source?
- charcircuit 3y ago
- viraptor 3y agoOnly in your controlled environment. When you publish something and it gets picked up by a distribution, it will fail. Sometimes even on a bug in a specific compiler version - Ubuntu had a GCC which sometimes picked up a `write()` result as unused and you couldn't ignore/dismiss in any way, so I had to carry patches for some project.
- forrestthewoods 3y agoIf the project is an “executable” then the toolchain dependency should be explicit. If it’s a “library” then it should be implicit. But it’s really not hard to support Clang + GCC + MSVC. And if new toolchains introduce new warnings they should be fixed. That said, my preference is for “dev” builds to not have warnings as errors, only “retail” which gets built by CI. Third party libraries almost always have to be compiled without warnings as errors because almost all open source code is rife with warnings.
- tonyarkles 3y ago> If the project is an “executable” then the toolchain dependency should be explicit. Cross-platform makes this fun. I work on a project that, in production, runs on an embedded Linux box. With a couple of features disabled, I can do development work just fine on my Macbook. Unfortunately, the embedded target uses a relatively old and crusty version of GCC and OS X has a newer version of Clang. We generally shoot for "zero warnings on production builds" but warnings in development on OS X are... okay. We pay attention to them and have occasionally benefitted from warnings that Clang provides that the older GCC does not, but some of the warnings come from some (admittedly questionable but work correctly) casts that a 3rd party library's header makes. Definitely not worth the effort to try to get upstream to fix the code gen that makes these headers to make OS X Clang happy.
- forrestthewoods 3y agoI do a lot of Linux + macOS + Windows support. Yeah third-party libraries are effectively a lost cause. It's ok if it's limited to cpp files. If it's in headers it's annoying. We vendor all third-party dependencies but would probably bias towards fixing the warning. It just spirals out of control too fast if you don't. Ancient toolchains for embedded or similar is definitely tough. If you can use modern toolchains then the combination of warnings across GCC + Clang + MSVC is probably a win.
- monocasa 3y agoI would make a 'make dev' target with stuff like -Werror
- isilofi 3y agoExactly. -Werror for the normal make target is problematic. But -Werror for a dev target that e.g. CI uses to compile-test pull requests on known toolchains and distros that you explicitly intend to support is useful. The alternative would be tons of effort to skip -Werror but still have CI flag that pull request that introduces new warnings. Just having the build fail because -Werror is far simpler and less fragile.
- remram 3y agoYeah, use it with your CI or test suite. Also `-Wd` for Python, and I'm sure there are equivalents for other toolchains/languages. IMHO it's similar to the robustness principle/Postel's law: "be conservative in what you send, be liberal in what you accept". Your build should pass without warnings on your system, but your build system should allow warnings on your customer's system.
- sgerenser 3y agoWithin a company (at least in my experience) compilers/toolchains are changed infrequently, usually less than once a year. In this case, keeping the software building with -Werror is a heck of a lot easier than allowing warnings to creep in, then trying to fix it later. It ends up that “later” never happens. Using -Werror from the very beginning forces you to address warnings: either by silencing obviously false positives, or fixing legitimate problems. A previous company I worked at had large C++ projects that built with 5000+ warnings. Nobody looked at them, obviously. Buried within them I recall one about printf-style formatting string mismatching arguments, that said something like “mismatching arguments will cause a crash if this code is executed.” I only noticed it buried in the list of warnings after debugging a segfault caused by that line of code.
- ninkendo 3y ago> either by silencing obviously false positives (Off-topic) Oh how I wish this were possible in the language I use at $dayjob. Swift does not let you silence warnings on a line by line basis. It’s easily one of the top 5 worst things about the language.
- saagarjha 3y agoThis would allow you to make a dialect where you call a function that Apple in their infinite wisdom has deprecated. Consider the tax this would impose on the ecosystem!
- saagarjha 3y agoI feel like every person who advocates for -Werror shows up to a codebase with a billion warnings once and then assumes that nobody can ever voluntarily keep their code compiling without warnings if they don’t have the build stop immediately so they can fix it. I’ve worked on dozens of non-legacy codebases and in every single one we don’t have warnings last for long, because going from zero to a few warnings is annoying. I don’t want that spam in my logs and I definitely don’t want suspicious constructs in my codebase. For the ones that enable -Werror, by the way, I always disable it so I can work and clean things up before committing. It’s just basic hygiene and discipline, the same as formatting your code before sending a PR. If your team doesn’t have that then it has bigger problems.
- orwin 3y agoIt's good clickbait. I think i was tired and wanted to get outraged (well, what's count as outraged to me, meaning shaking my head disapprovingly), i clicked on it then manage to agree with every points, and manage to learn something new with the "Refined Warning-As-Error Control" paragraph. Thank you author!
- emidln 3y agoReifying the toolchain dependency is the point. If you don't do this, you're fucked anyway.
- ploxiln 3y agoSo, how have linux distros worked for the past 25 years? Many independent projects write commonly used libraries and/or tools in C, and then each distro takes those and builds them with a slightly (or significantly!) different toolchain ... obviously it has worked pretty well. The pressure to work for all reasonable toolchains is good for software quality, instead of depending on quirks of a very particular toolchain to mask some bugs or nonstandard behavior dependency.
- emidln 3y agoYou bump the version gcc.spec in say, Fedora rawhide. What happens when random package foo doesnt compile with it? Foo is fixed or you skip that gcc version. Unless foo is very important and very hard to fix, you fix foo. Distros already enforce a consistent toolchain. Arch, Debian, Fedora, Gentoo, etc.
- ploxiln 3y agoA distro uses a consistent toolchain, but doesn't write most of the code. The projects writing the vast bulk of the code, like linux, git, firefox, gtk, qt, openssl, curl, etc, don't strictly control the toolchain, and it does vary between distros and distro releases. Distros like Arch and Alpine keep the patching to a minimum, most packages need no patches. RedHat/Fedora and Ubuntu do patch significantly more. Debian is in the middle ... there was an infamous incident where they patched openssl to fix an uninitialized-memory warning, and caused a huge security issue for many people: https://www.schneier.com/blog/archives/2008/05/random_number_b.html https://www.schneier.com/blog/archives/2008/05/random_number... (yes, openssl sucks, but still.) So this is why distros take their cleanup patches upstream. Distro patches are for real observed bugs that aren't fixed in an upstream release (or not in the desired major release branch), or to just disable -Werror in projects that don't know better.
- lmm 3y agoWhat you really want for working on a large scale codebase is "don't allow changes that introduce new warnings". And with current tooling that's something you can only really enforce at CI level if at all, but it shouldn't be that way. I'd like to have the same thing early in the test-edit cycle - compiling with a newer compiler that adds more warnings shouldn't be an error, but a code change that triggers a new warning should be.
- notnmeyer 3y agoseems like -Werror isn’t the problem as much as not having a codified/reproducible build toolchain is. there’s so many ways to skin that cat.
- bluGill 3y agoIn open source you don't get that option. Where you have a known set of build tools it is easy to use -werror, and if a new toolchain is added someone gets assigned to clean it up.
- ryandrake 3y agoThis only works if the maintainer cares enough. I’ve encountered maintainers who, bizarrely, are strict with “no warnings” but only when it comes to their preferred toolchain. Me: “Here’s a patch to get your project to compile cleanly using the Green Hills compiler targeting MIPS.” Maintainer: IDGAF. Rejected.
- dale_glass 3y agoI've not been in the situation yet, but it makes sense to me. Accepting a patch like that can be a significant commitment of time and brain power. "What exactly does this fix? Does it actually fix it?". Such patches can be a lot more complicated than a straightforward new feature. And then there's that oddball compilers may not support everything gcc and clang do, so making them happy may actually mean sacrificing something.
- bluGill 3y agoThatis a different situation. In commercial softeware you know your compiler and (if you care) you assign someone to fix the warnings on every upgrade. In open source you never know what compiler someone will use. You can probably say gcc 3.0 isn't supported, but someone might have just built the latest master clang with a new warning added.
- CamperBob2 3y ago-Werror is the least of your problems if you are compiling older C code in a newer GCC-based toolchain. Seemingly as a matter of policy, GCC has started playing very fast and loose with undefined behavior, treating it as a license to emit any code it wants or none at all, regardless of what most people would say the programmer "obviously" intended. There's a good chance your old code will compile if you don't use -Werror, or even if you do, but whether it runs the way it used to is a different question entirely.
- josephcsible 3y ago> Seemingly as a matter of policy, GCC has started playing very fast and loose with undefined behavior, treating it as a license to emit any code it wants or none at all, regardless of what most people would say the programmer "obviously" intended. But isn't that exactly what the C standard has said Undefined Behavior is, for as long as it has existed?
- CamperBob2 3y agoSigh. Yes, but here on Planet Earth, a lot of legacy code was (irresponsibly) written under the assumption that the contemporary compilers wouldn't change their behavior in the future. Also here on Planet Earth, performance optimization has become much less important than not breaking older code that previously worked, but nobody told the GCC devs. Never change, HN. Keep working toward a Utopia that never was and never will be. And keep downvoting helpful hints indicating additional things to watch out for in the context of the article.
- arp242 3y agoPrevious poster just asked a question; not everyone deals with C code, or old C code. I agree there's sometimes a certain lack of pragmatism, and that UB is used as the ultimate argument that trumps all arguments. But on the other hand, it really is UB, so that seems more of an argument for "we want a migration path" rather than "never change it until the end of time".
- 3y ago
- nathan_compton 3y agoDoesn't uh your build chain introduce a build chain dependency? Like each compiler compiles differently anyway, so if you care enough about warnings to have a zero warning policy, then don't you also care about it on every platform/build chain? I don't get this.
- mikepurvis 3y agoAs someone in DevOps with very similar frustrations to those expressed in TFA, I think my main issue with it comes when the people maintaining the software (who added Werror) aren't the ones spearheading the migration to a new Ubuntu LTS and therefore a new toolchain. That centralized strike force migration team now has to either fix everyone else's piddly deprecations for them, or else have a way off temporarily patching out Werror until that work can be handed back to the original maintainers.
- dagmx 3y agoA lot of projects use something like CMake to abstract that away. And even if ignore MSVC specific arguments, you’d still have to deal with multiple versions and variants of clang + gcc
- deleted 3y ago[deleted]
- wyldfire 3y agoIt'd be cool if I could have a code review view that's instrumented with output from static analyzers. I know some places have this (FB/Meta?) but I don't think it's a very "out of the box" kinda setup.
- humanrebar 3y agoWhat about the GitHub checks view and API?
- fh973 3y agoYou can set this up with gerrit.
- zX41ZdbW 3y agoWe use -Wall -Werror -Wextra -Weverything, plus we use clang-tidy with -Werror as well, plus we use ASan, TSan, MSan, UBSan, plus fuzzing (5+ different types of it), plus we update to the latest compiler on every release, so today it is clang-17, and we build every dependency from source, and always use hermetic builds (cross-compiling). I think it is the only sane way for C++. https://github.com/ClickHouse/ClickHouse/ https://github.com/ClickHouse/ClickHouse/
- astrange 3y ago-Weverything is explicitly not supported in clang and not supposed to produce working results. Same in gcc, it will give you wrong warnings like -Wstrict-aliasing.
- jandrewrogers 3y agoI don’t do -Weverything but generally most of the rest. I am in the maximum build hygiene camp, which means builds have to work across GCC and Clang, and x86-64 and ARM64, or they fail in CI. It finds quite a few compiler bugs as well as subtle code bugs.
- benreesman 3y agoI pretty frequently argue that memory safety (etc) outcomes in modern C++ are pretty comparable to Rust, but that’s only true if you’re throwing clang-tidy, -Werror, -Wall, -Wpedantic, and CI under ASAN, MSAN, UBSAN, TSAN. That’s pretty close to rustc. And I get that you can’t throw that at a giant codebase in one day, and I get that dev builds probably drop —Werror. Thrrr are realities. But new stuff not under clang-tidy, cppcheck, and pvs if you’re feeling speedy? People do that?
- AlotOfReading 3y agoThat's simply not true. All of those tools are regularly used by large projects like chrome and have been for years. Memory safety continues to be the largest source of issues for them in spite of that. Sanitizers and other modern tools are a fantastic improvement over the old way of doing things, but they don't catch everything and we shouldn't pretend they do.
- skybrian 3y agoThis is better understood when writing websites. A website shouldn’t be tied to a particular browser vendor or a particular browser version. Browsers don’t have any mechanism similar to -Werror because it would be a “break if the user upgrades their browser” flag. (There is “use strict” but it doesn’t become stricter with browser upgrades.) Similarly, let’s say we’re talking about an open source project that users may download and compile from source. A portable open source project shouldn’t needlessly break if you use a newer or different compiler. So: - Do make the source code as clean as you can, using all tools available, including -Werror. Putting -Werror in your continuous build should be fine too. - When a user downloads your library and compiles it in order to use it, the make target (or equivalent) that they use shouldn’t compile with -Werror, because we don’t know which compiler they have or which warnings that enables.
- 38 3y ago> aws.c:158:1: warning: control reaches end of non-void function [-Wreturn-type] I prefer modern languages like Go, because all these warnings and errors are just part of the default "go build": package hello func world() int { println("world") } // hello.go:5:1: missing return
- kazinator 3y agoYou should identify specify diagnostics that represent situations you don't want in your code. Then use -Werror=<warning-name> for each such diagnostic. The problem there is that GCC fails if you give it an option it doesn't recognize, including just a diagnostic one. If you use -Werror=funky-new-warning, it will fail on an older GCC that doesn't have funky-new-warning.
- IshKebab 3y agoBlacklisting specific warnings is a bad idea. Instead you should `-Werror` all warnings by default and then whitelist specific ones with `-Wno-foo` of `-Wno-error=foo` if you don't care about them. To handle differences in GCC/Clang flags you just have to detect the compiler (trivial with CMake), or dictate the compiler. But only do that for your developers and CI. Don't use `-Werror` for downstream users of your project.
- deleted 3y ago[deleted]
- kazinator 3y agoSay I've decided in my project that shadowing is evil. Why wouldn't I turn on -Werror=shadow? Where is the bad idea? Including downstream. I don't want people writing me patches that trigger my pet warnings. This is entirely compatible with the idea of locally only turning on -Werror, and then disabling certain warnings that you don't intend to fix, without foisting -Werror onto downstream. You can still have non-negotiable warnings that you turn on rather than off, and those do go downstream too.
- _ZeD_ 3y agothe real gist here is >>> We primarily use Clang, and it is a common occurrence that our CI server will report a warning when compiling our “warning-free” code with GCC. ... my only point here is: FIX YOUR TOOL CHAIN why do you want to use Clang if all your dependency relies on GCC?
- packetslave 3y agoGoogle used to compile development builds with clang by default (better tooling and error messages) but production builds with GCC (better optimization). I don't know if they still do that.
- kstenerud 3y agowerror is absolutely your friend. The examples given in the article are borderline strawmen. Changing toolchains is a very rare occurrence because the subtle differences will almost always cause problems. When someone suggests changing toolchains, they owe everyone a VERY CONVINCING argument. After which the changed warnings will be the LEAST of your problems as you migrate. Toolchain upgrades will cause new warnings to surface as the detection gets better. But you DO want to fix them, don't you? You ARE a professional, aren't you? You DO care about your product's quality, don't you? After over a decade without werror, followed by a decade with werror, I'd take werror every time. Warnings exist for a reason, and in the case of C compilers, VERY GOOD reasons that you really should address. At my workplace, we use werror, static analysis, various lints, detekt... everything that can issue warnings about your code quality. If any of them complain, you can't merge your PR, because we're professionals.
- pasc1878 3y agoIn the 1990s I think it was recommended for at least C++ to use at least two different compilers to catch as many errors as possible or to get a more understandable error message. linters and staic anaylsis did not really exist then
- tobias3 3y agoYou can do that if you have full control over the toolchain. For example I ran into this issue with the AWS C++ SDK ( https://github.com/aws/aws-sdk-cpp/ https://github.com/aws/aws-sdk-cpp/ ). And then I had to go into several CMake files and remove Werror. What do I as a user care about warnings? Can I only use that SDK once I have fixed (probably more like silenced) all the warnings? At least they added a option now after a few years of this annoyance: https://github.com/aws/aws-sdk-cpp/issues/2555 https://github.com/aws/aws-sdk-cpp/issues/2555
- skybrian 3y agoI’m guessing you haven’t been in a situation where the problems are apparent? Your advice probably works fine for small projects. You can upgrade the toolchain and fix all the new warnings in the same commit. Often there’s nothing to do. It doesn’t work for a monorepo where the toolchain needs to be upgraded for many projects at once. Enabling each new warning (turning it into a build-breaking error) may require touching hundreds of files. Getting a toolchain upgrade done in one monster commit doesn’t work; you will inevitably break something and have to roll back, or be unable to commit it all due to merge conflicts with other commits. I’ve been on a team that needed to be able to upgrade the toolchain in one commit and then turn on each new warning in separate commits. Having an explicit list of all the warnings that are turned on and then gradually updating it to be the equivalent of -Wall was the way to go.
- grumbel 3y agoEnable -Werror for the reproducible builds, leave it off for the rest. That should solve most of it. The biggest and rather frustrating issue left is that warnings are heavily tool depended, not just in that new tools will produce new warnings, but in that different tools will produce incompatible warning. The workaround that makes the warning go away in one tool, will create a warning in the other. You can end up with a lengthy back&forth before you find something that makes all the tools happy and you frequently run into situation were you are really just pleasing the tools, not fixing any actual issues.
- DrBazza 3y agoThat’s a lot of words to just say build in a container.
- Too 3y agoI don’t understand the OSS argument. Warnings spilling over from library dependencies can be mitigated easily by including them using -isystem instead of -I. For code that is not libraries, there is nothing strange about having dependency on a specific toolchain, preferably two supported options. Besides, warnings or not, devs using different toolchains than CI is just inviting for works-on-my-machine situations the rest of us left behind in the last century.
- flohofwoe 3y agoFor my own code I use "-Wall -Wextra -Werror" + a handful of warnings that are not in this set (like -Wsign-conversion) - and a similar draconian warning set on MSVC, both when building locally during development and on CI. But since my libraries don't ship with build system files those build process decisions are my own and don't "leak" to the users of my libraries (e.g. it wouldn't even enter my mind to dictate a specific build system or build settings on my library users, I know how bad it is to deal with overengineered cmake files of other people). When integrating other libraries I usually also just throw away the 1000+ lines of cmake files they come with and replace them with a few dozen lines of cmake script of my own (I wish that would be an exaggeration or a joke, but it's not - especially when it comes to "professional" C++ libraries coming out of Khronos or Google).
- perryizgr8 3y agoThe main point of the article is: > This flag adds a toolchain dependency to your project. Newer compiler versions (or alternative compiler vendors) are likely to generate new warnings, making the build fail. IMO most large projects will anyway have multiple toolchain dependencies. Ever tried to compile Chrome using a different compiler/build system? And why would different contributors use different toolchains anyway? If you want to contribute, just use the same tools everyone else on the project uses. When upgrading compilers, incompatible warnings are going to be the least of your worries. So what is the problem that the author wants to solve by not using -Werror? He agrees that warnings should not be merged. So why not stop them right when they're written?