如何使用 nanopb 和 protobuf 将 nanopb .proto 文件编译成 .h 和 .c 文件(`protoc` 编译问题)

How to compile nanopb .proto file into .h and .c files using nanopb and protobuf (`protoc` compile question)

旧标题:如何使用 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. But, what if I follow the Python installation instructions too? https://github.com/protocolbuffers/protobuf/tree/master/python

所以,这就是我所做的。

TLDR;也执行 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 现已创建!

现在构建 "simple" 项目:

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