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/...
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 the comments below this answer:
That'll get you going, but turning of a warning about potentially shady
reinterpret_casts
kinda gives me the heebie-jeebies.
– user4581301
Sep 29, 2020 at 0:10It's not for production code. Just for gathering some
size
output data on compiled binaries using various techniques. I agree: disabling this particular warning is a BAD idea for production code. But, this answer is meant to be a generic example of how the process would look for more benign warnings, and for that purpose, this answer is just what I need.
– Gabriel Staples
Sep 29, 2020 at 0:13
gcc
or clang
compilers)", here. Read there to learn more.#pragma
statements above and below header file #include
statements to affect just those files.