375 字
2 分钟
protoc版本问题

protobuf版本问题#

问题描述:#

在使用gRPC时,需要使用到protobuf,由于笔者安装过protobuf,版本为3.21.9,所以直接使用环境变量默认的protoc.exe编译了message文件以及生成对应的gRPC文件。结果报错如下:

This file was generated by a newer version of protoc which is incompatible with your Protocol Buffer headers. Please update your headers.

排查过程:#

这个问题由生成的message.pb.h头文件的#if预编译指令检查输出,经检查为:

#if PROTOBUF_VERSION < 3021000
#error This file was generated by a newer version of protoc which is
#error incompatible with your Protocol Buffer headers. Please update
#error your headers.

由此可以知道,当前文件中定义的PROTOBUF_VERSION小于3021000,而笔者的protobuf版本为3.21.9,对应的PROTOBUF_VERSION3021001,所以报错。

由于PROTOBUF_VERSION是指当前使用的libprotobufd库的版本号,而不是protoc编译器的版本号,所以需要检查当前使用的libprotobufd库的版本。

经过检查设置->Linker->include发现,在当前项目的配置中,使用的是gRPC\third_party\protobuf\Debug\libprotobufd.lib,对应的源码在D:\dev\gRPC\third_party\protobuf\src\google\protobuf\stubs\common.h中,查看源码发现这个库的版本为3.13.0,与protoc编译器的版本不一致。

对应的源码如下:

// The current version, represented as a single integer to make comparison
// easier: major * 10^6 + minor * 10^3 + micro
#define GOOGLE_PROTOBUF_VERSION 3013000
// A suffix string for alpha, beta or rc releases. Empty for stable releases.
#define GOOGLE_PROTOBUF_VERSION_SUFFIX ""

解决方法#

  • 选择gRPC生成的protoc编译器:使用gRPC\third_party\protobuf\Debug\目录下找到对应版本protoc.exe程序,编译生成.pb.h文件和gRPC.pb.h文件。
protoc版本问题
https://chrisnake11.github.io/blog/posts/coding/chatroom/protoc版本问题/
作者
Zheyv
发布于
2025-07-09
许可协议
CC BY-NC-SA 4.0