如何解决protobuf_generate grpc_cpp_plugin 失败,状态码为 1
我正在尝试生成 grpc cpp 文件,但是当我在 cmake 中调用它时生成器插件失败
find_package(protoc CONFIG REQUIRED)
find_package(gRPC CONFIG REQUIRED)
get_target_property(grpc_cpp_plugin_location gRPC::grpc_cpp_plugin LOCATION) #get plugin location
get_target_property(protoc_location protobuf::protoc LOCATION) #for sanity check
message("protoc is at ${protoc_location}") #for sanity check
message("grpc plugin is at ${grpc_cpp_plugin_location}") #for sanity check
add_library(${PRO_NAME} ${PRO_SRCS} test.proto)
protobuf_generate(TARGET ${PRO_NAME} LANGUAGE cpp)
protobuf_generate(TARGET ${PRO_NAME} LANGUAGE grpc GENERATE_EXTENSIONS .grpc.pb.h .grpc.pb.cc PLUGIN "protoc-gen-grpc=${grpc_cpp_plugin_location}")
产生像
这样的错误protoc is at /home/user/.conan/data/protoc_installer/...
grpc plugin is at /home/user/.conan/data/grpc/...
-- Configuring done
-- Generating done
-- Build files have been written to:
[ 16%] Running grpc protocol buffer compiler on test.proto
protoc-gen-grpc: program not found or is not executable
Please specify a program using absolute path or make sure the program is available in your PATH system variable
--grpc_out: protoc-gen-grpc: Plugin failed with status code 1
但是如果我在 shell 脚本中手动使用它,即使我从 cmake 输出复制文件路径,它也能工作
PROTOC=${PATH FROM CMAKE}
GRPCC=${PATH FROM CMAKE}
$PROTOC -I src/ --grpc_out=. --plugin=protoc-gen-grpc=$GRPCC test.proto
基于 https://www.falkoaxmann.de/dev/2020/11/08/grpc-plugin-cmake-support.html 的方法
我最初认为我的问题是库路径错误,我使用的是带有 conan 导入管理的远程构建服务器,但我确认插件 rpath 包含它需要的库的相对位置。
解决方法
问题可能是 cmake 使用了过时的 protobuf 配置,或者更确切地说是 cmake 附带的 FindProtobuf.cmake
而不是 protobuf 包附带的 ProtobufConfig.cmake
。
要解决此问题,请自行编译 gRPC 并在为 gRPC 运行 cmake 时使用 -DgRPC_PROTOBUF_PACKAGE_TYPE=CONFIG
标志。
问题来自 gRPC 在其 gRPCConfig.cmake 中有自己的 find_package(Protobuf)
调用,根据 gRPC_PROTOBUF_PACKAGE_TYPE
,它可能会在 MODULE 模式下运行。 (在模块模式下,它会找到遗留的 FindProtobuf.cmake)
查看最近添加的 github 工作流以获取有关如何构建依赖项的示例以及示例: https://github.com/faaxm/exmpl-cmake-grpc/blob/master/.github/workflows/build.yml
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。