Issue
I'm getting the following error when building:
...has undefined behavior [-Werror,-Wundefined-reinterpret-cast]
The Bazel build completely stops since this clang (llvm compiler) -Wundefined-reinterpret-cast
warning is converted into a build error by -Werror
.
How can I force the build to continue and produce a binary executable despite this build error?
Note that my bazel build command has this form:
time bazel build //my/src/...
Solution
The answer is to use the -Wno-error=<name>
build flag, as described by gcc here (note that clang's options are modeled after gcc):
-Werror=
Make the specified warning into an error. The specifier for a warning is appended; for example
-Werror=switch
turns the warnings controlled by-Wswitch
into errors. This switch takes a negative form, to be used to negate-Werror
for specific warnings; for example-Wno-error=switch
makes-Wswitch
warnings not be errors, even when-Werror
is in effect.The warning message for each controllable warning includes the option that controls the warning. That option can then be used with
-Werror=
and-Wno-error=
as described above.
Source: https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html (emphasis added).
So, for this case, add the build option -Wno-error=undefined-reinterpret-cast
to turn OFF the -Werror=undefined-reinterpret-cast
flag.
In Bazel, you can pass C/C++ build options with the --copt="<flag>"
option (see here) (see also the --per_file_copt
option (see here and here)), making the final command look like this for this case:
time bazel build --copt="-Wno-error=undefined-reinterpret-cast" //my/src/...
This works! The Bazel build now runs to completion, showing these problems only as warnings again (notice -Werror
is missing from the warning statement now):
...has undefined behavior [-Wundefined-reinterpret-cast]
Note that if you need to pass multiple build flags in at once, use multiple calls to --copt=
. Ex:
time bazel build --copt="-Wno-error=undefined-reinterpret-cast" \
--copt="-Wno-error=switch" --copt="-ggdb" --copt="-O0" //my/src/...
Note: don't ever do this on production code for potentially-serious warnings like this (ex: undefined behavior). For more benign warnings, this is the right technique if you really need to disable one. For undefined behavior, this should just be for learning. See my comment below this answer.
More reading:
- I've documented a lot of the above information, and more, in my eRCaGuy_hello_world repo under the section titled "Additional C and C++ build notes (ex: w/
gcc
orclang
compilers)", here. Read there to learn more. - [I STILL NEED TO TRY AND TEST THIS OUT] https://nelkinda.com/blog/suppress-warnings-in-gcc-and-clang/ - see esp. Section "3.3 Suppressing Warnings by Controlling the Diagnostic Stack". Learn to enable/disable GCC and Clang compiler warnings and options just for certain files or sections of code. Consider putting the necessary
#pragma
statements above and below header file#include
statements to affect just those files.
Answered By - Gabriel Staples