问题描述
构建时出现以下错误:
...has undefined behavior [-Werror,-Wundefined-reinterpret-cast]
由于-Werror
将此clang (llvm compiler) -Wundefined-reinterpret-cast
warning转换为构建错误,Bazel构建完全停止。
尽管出现此构建错误,如何强制构建继续并生成二进制可执行文件?
请注意,我的bazel构建命令具有以下形式:
time bazel build //my/src/...
解决方法
答案是使用-Wno-error=<name>
构建标记as described by gcc here(请注意,clang的选项是在gcc之后建模的):
-Werror=
使指定的警告变为错误。附加了警告说明符;例如
-Werror=switch
会将-Wswitch
控制的警告变成错误。 此开关采用否定形式,用于对特定警告取反-Werror
;例如-Wno-error=switch
会使-Wswitch
的警告变成错误,即使-Werror
有效。每个可控制警告的警告消息均包含控制警告的选项。如上所述,该选项然后可以与
-Werror=
和-Wno-error=
一起使用。
来源:https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html(添加了重点)。
因此,在这种情况下,添加构建选项-Wno-error=undefined-reinterpret-cast
以关闭-Werror=undefined-reinterpret-cast
标志。
在Bazel中,您可以将C / C ++构建选项与--copt="<flag>"
选项一起传递(请参见here)(另请参见--per_file_copt
选项(请参见here和{{ 3}})),在这种情况下,使最终命令如下所示:
time bazel build --copt="-Wno-error=undefined-reinterpret-cast" //my/src/...
这有效! Bazel构建现在运行完成,仅将这些问题再次显示为警告(警告声明中现在缺少-Werror
通知):
...has undefined behavior [-Wundefined-reinterpret-cast]
请注意,如果您需要一次传递多个构建标志,请对--copt=
使用多个调用。例如:
time bazel build --copt="-Wno-error=undefined-reinterpret-cast" \
--copt="-Wno-error=switch" --copt="-ggdb" --copt="-O0" //my/src/...
注意:永远不要在生产代码上针对此类潜在的严重警告执行此操作(例如,未定义的行为)。对于更有益的警告,如果您确实需要禁用警告,则这是正确的技术。对于不确定的行为,这应该只是为了学习。请参阅此答案下方的我的评论。