c - How to compile nanopb .proto file into .h and .c files using nanopb and protobuf (`protoc` 编译问题)

标签 c protocol-buffers protoc protobuf-c nanopb

旧标题:如何使用nanopb和protobuf将nanopb/examples/simple/simple.proto文件编译成simple.h和simple.c

关于这个库:https://github.com/nanopb/nanopb

我的目标是遵循本教程:https://jpa.kapsi.fi/nanopb/docs/concepts.htmlnanopb/examples/simple/simple.proto 转换为 .h 和 .c 源文件。我需要简单的说明才能在 Ubuntu 上执行此操作。我已经尝试了好几天了,但无法让它发挥作用。

本教程说要执行的命令是:

protoc -omessage.pb message.proto
python ../generator/nanopb_generator.py message.pb

我克隆了 nanopb 存储库,cd 到 nanopb/examples/simple,然后替换为 simple.proto 而不是 message.proto 在上面的命令中,我运行了以下命令:

protoc -osimple.pb simple.proto

它运行良好,生成了一个 simple.pb 文件。

然而,第二部分失败了。从 nanopb/examples/simple 文件夹中运行时,我得到:

$ python ../../generator/nanopb_generator.py simple.pb

         ********************************************************************
         *** Failed to import the protocol definitions for generator.     ***
         *** You have to run 'make' in the nanopb/generator/proto folder. ***
         ********************************************************************

Traceback (most recent call last):
  File "../../generator/nanopb_generator.py", line 39, in <module>
    import proto.nanopb_pb2 as nanopb_pb2
  File "/home/gabriels/GS/dev/Protocol_Buffers/Nanopb/source/nanopb/generator/proto/nanopb_pb2.py", line 11, in <module>
    from google.protobuf import symbol_database as _symbol_database
ImportError: cannot import name symbol_database

运行 make 什么都不做(说它已经完成):

nanopb/generator/proto $ make
make: Nothing to be done for `all'.

请注意,我正在运行最新版本的 protoc,它是从 Google protobuf 存储库构建的,来源为:https://github.com/protocolbuffers/protobuf .

我也曾在这里寻求 nanopb 的帮助,但我无法弄清楚,感觉这里缺少一些基本的东西,因为我知道的不够多:https://github.com/nanopb/nanopb/issues/417 .感觉我正在努力做一些应该很简单并且在我之前至少有 1448 多人已经完成的事情(nanopb 上的星星数量)。

最佳答案

已解决。 @PetteriAimonen 有 given me the missing clue :

the protoc version needs to match with the python library version

然后我想到:最初,当从头开始编译 protobuf 时,我只遵循 C++ 安装说明,如下所示:https://github.com/protocolbuffers/protobuf/tree/master/src .但是,如果我也遵循 Python 安装说明怎么办? https://github.com/protocolbuffers/protobuf/tree/master/python

所以,这就是我所做的。

长长的;也执行 protobuf 库的 Python 安装(不仅仅是 C++ 安装):

我遵循的 Protobuf Python 安装步骤:

python -V # See if I have Python 2.7 or newer (I must to continue)
cd protobuf/python # cd into Python source directory
python setup.py build
python setup.py test
(cd .. && make)
(cd .. && sudo make install)
python setup.py build --cpp_implementation
python setup.py test --cpp_implementation  # look to see all tests pass
sudo python setup.py install

.proto 文件的两步编译:

一切正常,所以现在让我们返回并再次尝试编译我们的 simple.proto 文件。

cd 进入 nanopb/examples/simple。我们已经运行了第一个命令来生成 simple.pb 文件,所以现在只需运行之前会失败的第二个命令,它就可以工作了!

仅第二个命令:

nanopb/examples/simple $ python ../../generator/nanopb_generator.py simple.pb

输出:

nanopb/examples/simple $ python ../../generator/nanopb_generator.py simple.pb  
Writing to simple.pb.h and simple.pb.c

为完整起见再次显示 2 个命令:

protoc -osimple.pb simple.proto
nanopb/examples/simple $ python ../../generator/nanopb_generator.py simple.pb

漂亮!有效! simple.pb.hsimple.pb.c 现已创建!

现在构建“简单”项目:

make

然后运行它:

./simple

输出是:

nanopb/examples/simple $ ./simple  
Your lucky number was 13!

现在我可以研究这个项目,看看 simple.proto 是如何变成 simple.pb.hsimple.pb.c ,我可以研究 simple.c(其中包含 main() 函数)以查看这些自动生成的 .h 和 .c 文件的完整用法,包括查看在它包含的以下头文件中:

#include <pb_encode.h> # found up 2 levels, in "nanopb" folder
#include <pb_decode.h> # found up 2 levels, in "nanopb" folder
#include "simple.pb.h" # just generated right here in "nanopb/examples/simple" folder

构建 .proto 文件的一行命令:

不是执行两行命令来构建 .proto 文件:

# From inside folder "/home/gabriels/GS/dev/Protocol_Buffers/Nanopb/source/nanopb/examples/simple":
protoc -osimple.pb simple.proto
python ../../generator/nanopb_generator.py simple.pb

我们可以执行一个单行命令来构建 .proto 文件,它只使用 protoc 可执行文件加上 protoc-gen-nanopb插件:

protoc --plugin=protoc-gen-nanopb=/home/gabriels/GS/dev/Protocol_Buffers/Nanopb/source/nanopb/generator/protoc-gen-nanopb --nanopb_out=. simple.proto

然后,当然,我们仍然需要制作并运行主要的 C 项目:

# From inside folder "/home/gabriels/GS/dev/Protocol_Buffers/Nanopb/source/nanopb/examples/simple":
make && ./simple

关于c - How to compile nanopb .proto file into .h and .c files using nanopb and protobuf (`protoc` 编译问题),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57367265/

相关文章:

javascript - 将 javascript 中的 Protobuf 模式转换为单独的原型(prototype)定义文件

python - 找不到 `protoc` 命令

grpc - 从不同的包导入 proto 文件

c - 如何在 Ruby 和 c 之间共享数据?

c - 如何在 C 中将包含 UTF-8 十六进制值的字符串转换为 wchar?

c - 在函数内更新 C 中的 (int) 变量

java - 如何在构建消息之前在 protobuf 中设置重复字段?

java - 套接字:将无符号字符数组从 C 传递到 JAVA

python - 如何通过python protobuf解析C++ protobuf二进制数据?

Golang - google/protobuf/Empty.proto : File not found