【问题标题】:How to compile nanopb .proto file into .h and .c files using nanopb and protobuf (`protoc` compile question)如何使用 nanopb 和 protobuf 将 nanopb .proto 文件编译成 .h 和 .c 文件(`protoc` 编译问题)
【发布时间】:2019-12-13 11:44:19
【问题描述】:

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

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

我的目标是遵循本教程:https://jpa.kapsi.fi/nanopb/docs/concepts.html 将 nanopb/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,我运行了以下命令: p>

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,它是从源代码:https://github.com/protocolbuffers/protobuf 的 Google protobuf 存储库构建的。

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

【问题讨论】:

    标签: c protocol-buffers protoc protobuf-c nanopb


    【解决方案1】:

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

    protoc版本需要与python库版本匹配

    然后突然想到:原来,我从头编译protobuf时,只按照C++安装说明,如下图:https://github.com/protocolbuffers/protobuf/tree/master/src。但是,如果我也按照 Python 安装说明进行操作怎么办? 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.h 和 simple.pb.c 现在已创建!

    现在构建“简单”项目:

    make
    

    然后运行它:

    ./simple
    

    输出是:

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

    现在我可以研究这个项目,看看 simple.proto 是如何变成 simple.pb.h 和 simple.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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-09
      相关资源
      最近更新 更多