使用Bazel构建时遇到Protobuf相关编译错误
Bazel构建报错:only virtual member functions can be marked 'override'解决方法
执行Bazel构建时触发如下编译错误:
In file included from external/com_google_protobuf/src/google/protobuf/compiler/main.cc:33:
external/com_google_protobuf/src/google/protobuf/compiler/java/java_kotlin_generator.h:59:41: error: only virtual member functions can be marked 'override'
uint64_t GetSupportedFeatures() const override;
^
已尝试删除${GOPATH}/bin中的protoc-gen-go,但问题仍存在。
针对该错误,可尝试以下解决步骤:
- 锁定兼容的protobuf版本:在WORKSPACE文件中指定一个与项目环境匹配的稳定protobuf版本,示例配置:
http_archive( name = "com_google_protobuf", urls = ["https://github.com/protocolbuffers/protobuf/releases/download/v21.12/protobuf-all-21.12.tar.gz"], sha256 = "635b6125d4e538f64054c2f7b9f57d304f0a7a73a07a5a349493c43f3a20a7e8", ) - 指定C编译标准:在相关BUILD文件中为protobuf目标添加C17及以上版本的编译选项,示例:
cc_library( # 其他配置项 copts = ["-std=c++17"], ) - 清理Bazel缓存:执行命令
bazel clean --expunge清除所有缓存文件后重新构建 - 检查grpc版本兼容性:若项目依赖grpc,确保其与protobuf版本匹配,避免跨版本冲突
内容的提问来源于stack exchange,提问作者Technical Shil
相关产品推荐
相关产品推荐

