无法为ARM交叉编译.proto文件,x86_64编译正常求助
交叉编译ARM架构.proto文件时grpc插件执行失败的解决方法
问题场景
开发C++项目,需为ARM架构交叉编译.proto文件,使用Yocto构建环境(配套带根文件系统的SDK及环境变量配置脚本)。交叉编译时选用x86_64版本的grpc_cpp_plugin,确保插件能在x86_64主机上正常执行。
报错信息
构建过程中,.pb.cc和.pb.h文件能正常生成(说明protoc工作正常),但.grpc.pb.h和.grpc.pb.cc文件缺失,同时抛出以下错误:
[build] protoc-gen-grpc: program not found or is not executable [build] Please specify a program using absolute path or make sure the program is available in your PATH system variable [build] --grpc_out: protoc-gen-grpc: Plugin failed with status code 1.
原CMake配置
cmake_minimum_required(VERSION 3.19) project(ProtoFiles) file(GLOB_RECURSE proto-files "${CMAKE_CURRENT_LIST_DIR}/" "*.proto") if(${CMAKE_SYSTEM_PROCESSOR} MATCHES "x86_64") set(_GRPC_CPP_PLUGIN_EXECUTABLE "${CMAKE_SOURCE_DIR}/third_party/x86_64/gRPC/bin/grpc_cpp_plugin") else() set(_GRPC_CPP_PLUGIN_EXECUTABLE "${CMAKE_SOURCE_DIR}/toolchain/sysroots/x86_64/usr/bin/grpc_cpp_plugin") endif() add_library(ProtoFiles OBJECT ${proto-files}) target_link_libraries(ProtoFiles PUBLIC protobuf::libprotobuf gRPC::grpc gRPC::grpc++) set(PROTO_BIN_DIR "${CMAKE_SOURCE_DIR}/build_proto") file(MAKE_DIRECTORY ${PROTO_BIN_DIR}) set(PROTO_IMPORT_DIRS "${CMAKE_CURRENT_LIST_DIR}/proto") target_include_directories(ProtoFiles PUBLIC "$<BUILD_INTERFACE:${PROTO_BIN_DIR}>") protobuf_generate( TARGET ProtoFiles IMPORT_DIRS ${PROTO_IMPORT_DIRS} PROTOC_OUT_DIR ${PROTO_BIN_DIR}) protobuf_generate( TARGET ProtoFiles LANGUAGE grpc GENERATE_EXTENSIONS .grpc.pb.h .grpc.pb.cc PLUGIN "protoc-gen-grpc=${_GRPC_CPP_PLUGIN_EXECUTABLE}" IMPORT_DIRS ${PROTO_IMPORT_DIRS} PROTOC_OUT_DIR "${PROTO_BIN_DIR}")
排查要点
- x86_64架构本地编译完全正常
- 手动在控制台执行protoc+grpc_cpp_plugin命令可正常生成grpc相关文件,路径配置无问题
- 使用的protoc版本为3.15.2
解决方案
将原CMake中调用protobuf_generate生成grpc文件的逻辑,替换为直接用execute_process调用protoc命令,修改后的代码如下:
set(PROTOC_COMMAND ${CMAKE_SOURCE_DIR}/toolchain/sysroots/x86_64/usr/bin/protoc) set(PROTOC_CMD_PATH --proto_path=${CMAKE_SOURCE_DIR}) set(PROTOC_GRPC_OUT --grpc_out=${PROTO_BIN_DIR}) set(PROTOC_PLUGIN --plugin=protoc-gen-grpc=${_GRPC_CPP_PLUGIN_EXECUTABLE}) execute_process( COMMAND ${PROTOC_COMMAND} ${PROTOC_CMD_PATH} ${PROTOC_GRPC_OUT} ${PROTOC_PLUGIN} ${proto-files} )
修改后重新构建,成功生成.grpc.pb.cc和.grpc.pb.h文件。
内容的提问来源于stack exchange,提问作者31ank
相关产品推荐
相关产品推荐

