Aussie AI
Warning-Free Compilation
-
Book Excerpt from "Generative AI in C++"
-
by David Spuler, Ph.D.
Warning-Free Compilation
Don't ignore compiler warnings! I'm not a fervent advocate of having a set of C++ coding style guidelines for a project, but I do feel strongly about this one. Modern compiler warnings are so good that it's like running a “linter” for free.
A very good goal for C++ software quality is to get to a warning-free compile.
You should think of compiler warnings as doing “static analysis” of your code,
which was an idea that started back on Unix with the C “lint
” tool, back in the
days before we had electricity.
To maximize this idea, turn on more warning options,
such as -Wall
for gcc
.
The warnings are rarely wrong in modern compilers,
although some are about harmless things.
Harmless doesn't mean unimportant. And anyway, the so-called “harmless” warnings aren't actually harmless, because if there's too many of them in the compilation output, then the bad bugs won't get seen. Hence, make the effort to fix the minor issues in C++ code that's causing warnings. For example, fix the “unused variable” warnings or “mixing float and double” type warnings, even though they're rarely a real bug. And yet, sometimes they are! This is why it's powerful to have a warning-free compile.
If you are a true believer in warning-free compilation,
you can elevate all GCC warnings to compilation errors using the “-Werror
” command-line option.
Personally, I don't recommend going this far, as some warnings are really tricky to get rid of.
Two compilers are better than one!
Another powerful quality trick is to compile your code on multiple compiler platforms.
For example, I try to write code that is portable enough that
it compiles on both the Microsoft Visual Studio C++ IDE for Windows and command-line GCC on Linux.
This is usually a matter of adding #ifdef
statements for Windows versus Linux.
By checking your code twice,
with a small overhead of a Makefile or Project file, you get warnings from both of these amazing C++ compilers.
It's like getting double-linted for free!
Tracking compilation warnings. One way to take warning-free compilation to the next level is to actually store and analyze the compiler output. It's like log file analysis in DevOps, only it's not for systems management. On Linux, I typically use this idea:
make build |& tee makebuild.txt
Here's an actual example from a Linux Makefile in an Aussie AI project:
build: -@make build2 |& tee makebuild.txt -@echo 'See output in makebuild.txt'
The Makefile uses prefix “-
” and “@
” flags, which means that it doesn't echo the command to output,
and doesn't stop if one of the steps triggers an error.
When the build has finished, then we have a text file “makebuild.txt
” which can be viewed for warning messages.
To go further, I usually use grep
to remove
some of the common informational messages, to leave only warning messages.
Typically, my Linux command looks like:
make warnings
Here's an example of the “warnings
” target in a Linux Makefile for one of my Aussie AI projects:
warnings: -@cat makebuild.txt | grep -v '^r -' \ | grep -v '^g++ ' | grep -v '^Compiling' \ | grep -v '^Making' | grep -v '^ar ' \ | grep -v '^make\[' | grep -v '^ranlib' \ | grep -v '^INFO:' | grep -v 'Regressions failed: 0' \ | grep -v 'Assertions failed: 0' | grep -v SUCCESS \ |more
Note that this uses grep
to remove the informational messages from g++
, ar
, ranlib
, and make
.
And it also removes the unit testing success messages if all tests pass (but not if they fail!).
The idea is to show only the bad stuff
because log outputs with too many lines get boring far too quickly
and then nobody's watching.
Finally, your warning-free tracking method should ideally be part of your “nightly builds” that do more extensive analysis than the basic CI/CD acceptance testing. You should email those warnings to the whole team, at about 2am ideally, because C++ programmers don't deserve any sleep.
• Next: • Up: Table of Contents |
The new AI programming book by Aussie AI co-founders:
Get your copy from Amazon: Generative AI in C++ |