简体   繁体   English

如何使用 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)

Old title: How to compile nanopb/examples/simple/simple.proto file into simple.h and simple.c using nanopb and protobuf旧标题:如何使用 nanopb 和 protobuf 将 nanopb/examples/simple/simple.proto 文件编译成 simple.h 和 simple.c

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

My goal is to follow this tutorial: https://jpa.kapsi.fi/nanopb/docs/concepts.html to convert nanopb/examples/simple/simple.proto into a .h and .c source file.我的目标是遵循本教程: https : //jpa.kapsi.fi/nanopb/docs/concepts.htmlnanopb/examples/simple/simple.proto转换为 .h 和 .c 源文件。 I need simple instructions to do this on Ubuntu.我需要简单的说明才能在 Ubuntu 上执行此操作。 I've been attempting it for days and am unable to get it to work.我已经尝试了几天,但无法让它工作。

The commands the tutorial says to do are:教程说要做的命令是:

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

I cloned the nanopb repo, cd'ed into nanopb/examples/simple , and then substituting in simple.proto instead of message.proto in the commands above, I ran the following:我克隆了 nanopb 存储库,将其放入nanopb/examples/simple ,然后在上面的命令中替换为simple.proto而不是message.proto ,我运行了以下命令:

protoc -osimple.pb simple.proto

It worked fine, producing a simple.pb file.它运行良好,生成了一个 simple.pb 文件。

The 2nd part, however, fails.然而,第二部分失败了。 When running from inside the nanopb/examples/simple folder, I get: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

Running make does nothing (says it's already done):运行make什么都不做(说它已经完成了):

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

Note that I am running the latest version of protoc , built from the Google protobuf repo from source: https://github.com/protocolbuffers/protobuf .请注意,我正在运行最新版本的protoc ,它是从 Google protobuf repo 的源代码构建的: https : //github.com/protocolbuffers/protobuf

I have also sought help from nanopb here, but am unable to figure it out, and feel like there's something basic here I'm missing because I just don't know enough: https://github.com/nanopb/nanopb/issues/417 .我也在这里向 nanopb 寻求帮助,但我无法弄清楚,并且觉得这里有一些基本的东西我错过了,因为我知道的还不够多: https : //github.com/nanopb/nanopb/issues /417 Feels like I'm beating my head into the wall on something that should be simple and has already been done by at least 1448+ people before me (the number of stars on nanopb).感觉就像我在一些应该很简单的事情上撞墙,并且在我之前已经至少有 1448+ 人做过(nanopb 上的星星数量)。

Solved.解决了。 @PetteriAimonen had given me the missing clue : @PetteriAimonen 给了我丢失的线索

the protoc version needs to match with the python library version protoc 版本需要与 python 库版本匹配

Then it occurred to me: originally, when compiling protobuf from scratch, I followed only the C++ installation instructions, as shown here: https://github.com/protocolbuffers/protobuf/tree/master/src .然后我突然想到:最初,从头开始编译 protobuf 时,我只遵循 C++ 安装说明,如下所示: https : //github.com/protocolbuffers/protobuf/tree/master/src But, what if I follow the Python installation instructions too?但是,如果我也遵循 Python 安装说明呢? https://github.com/protocolbuffers/protobuf/tree/master/python https://github.com/protocolbuffers/protobuf/tree/master/python

So, that's what I did.所以,这就是我所做的。

TLDR; TLDR; Do the Python installation of the protobuf library too (not just the C++ installation):也进行protobuf库的 Python 安装(不仅仅是 C++ 安装):

Protobuf Python installation steps I followed:我遵循的 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

Two-step compile of .proto files: .proto 文件的两步编译:

That all worked, so now lets go back and try compiling our simple.proto file again.这一切都奏效了,所以现在让我们返回并再次尝试编译我们的 simple.proto 文件。

cd into nanopb/examples/simple . cdnanopb/examples/simple We already ran the first command to produce the simple.pb file, so now just run the 2nd command that previously would fail, and it works!我们已经运行了第一个命令来生成 simple.pb 文件,所以现在只需运行之前会失败的第二个命令,它就可以工作了!

2nd Command only:仅第二个命令:

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

Output:输出:

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

2-commands shown together again for completeness:为完整起见,将 2 个命令再次显示在一起:

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

Beautiful!美丽的! IT WORKED!有效! simple.pb.h and simple.pb.c are now created! simple.pb.hsimple.pb.c现已创建!

Now build the "simple" project:现在构建“简单”项目:

make

And run it:并运行它:

./simple

And the output is:输出是:

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

Now I can study the project to see how simple.proto was turned into simple.pb.h and simple.pb.c , and I can study simple.c (which contains the main() function) to see a full usage of these auto-generated .h and .c files, including looking at the following header files which it includes:现在我可以研究项目,看看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

One-line command to build .proto files:构建 .proto 文件的一行命令:

Instead of doing the two-line command to build .proto files :而不是执行两行命令来构建 .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

we can do a one-line command to build .proto files which just uses the protoc executable plus the protoc-gen-nanopb plugin:我们可以做一个行命令构建.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

And then, of course, we still need to make and run the main C project:然后,当然,我们仍然需要制作并运行主 C 项目:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM