protobuf - Compile Proto File

本文介绍了如何正确配置环境变量并编译proto文件为C++和Python代码。通过解决libprotoc.so.14库找不到的问题,展示了如何修改ld.so.conf文件及使用ldconfig命令。此外还讲解了proto文件中语法版本的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

proto文件

下面是一个简单的proto文件。

/*
protoc -I=. --cpp_out=. helloworld.proto 
*/

message helloworld 
{ 
    required int32     id = 1;  // ID 
    required string    str = 2;  // str 
    optional int32     opt = 3;  //optional field 
}

compile proto file

$ protoc -I=. --cpp_out=. helloworld.proto
protoc: error while loading shared libraries: libprotoc.so.14: cannot open shared object file: No such file or directory
$

libprotoc.so

lib.protoc.so安装在/usr/local/lib目录,而该目录没有加到LD_LIBRARY_PATH中。解决主要步骤:

  • 在/etc/ld.so.conf.d/目录下创建一个conf文件;
  • 在该文件中增加一行: /usr/local/lib
  • sudo ldconfig

过程如下:

$ protoc -I=. --cpp_out=. helloworld.proto
protoc: error while loading shared libraries: libprotoc.so.14: cannot open shared object file: No such file or directory
$ ls /usr/local/lib/
libpcre.a                   libpcreposix.a              libpcre.so.1.2.3            libprotobuf-lite.so.14.0.0  libprotoc.so.14
libpcrecpp.a                libpcreposix.la             libprotobuf.a               libprotobuf.so              libprotoc.so.14.0.0
libpcrecpp.la               libpcreposix.so             libprotobuf.la              libprotobuf.so.14           pkgconfig/
libpcrecpp.so               libpcreposix.so.0           libprotobuf-lite.a          libprotobuf.so.14.0.0       python2.7/
libpcrecpp.so.0             libpcreposix.so.0.0.2       libprotobuf-lite.la         libprotoc.a                 python3.5/
libpcrecpp.so.0.0.0         libpcre.so                  libprotobuf-lite.so         libprotoc.la
libpcre.la                  libpcre.so.1                libprotobuf-lite.so.14      libprotoc.so
$ cat /etc/ld.so.conf
include /etc/ld.so.conf.d/*.conf

$ ls /etc/ld.so.conf.d/
fakeroot-i386-linux-gnu.conf  i386-linux-gnu.conf  i386-linux-gnu_EGL.conf  i386-linux-gnu_GL.conf  libc.conf
$ sudo vi /etc/ld.so.conf.d/protobuf.conf
[sudo] password for flying-bird: 
$ cat /etc/ld.so.conf.d/protobuf.conf 
/usr/local/lib
$ protoc -I=. --cpp_out=. helloworld.proto
protoc: error while loading shared libraries: libprotoc.so.14: cannot open shared object file: No such file or directory
$ sudo ldconfig
$ protoc -I=. --cpp_out=. helloworld.proto
[libprotobuf WARNING google/protobuf/compiler/parser.cc:546] No syntax specified for the proto file: helloworld.proto. Please use 'syntax = "proto2";' or 'syntax = "proto3";' to specify a syntax version. (Defaulted to proto2 syntax.)
$ 

syntax

可以在proto文件开头添加一行,说明语法版本。

/*
protoc -I=. --cpp_out=. helloworld.proto 
*/

syntax = "proto2";

message helloworld 
{ 
    required int32     id = 1;  // ID 
    required string    str = 2;  // str 
    optional int32     opt = 3;  //optional field 
}

重新编译:

$ protoc -I=. --cpp_out=. helloworld.proto
$ ls -l
total 40
-rw-rw-r-- 1 flying-bird flying-bird 20490 8月   7 23:14 helloworld.pb.cc
-rw-rw-r-- 1 flying-bird flying-bird 10651 8月   7 23:14 helloworld.pb.h
-rw-rw-r-- 1 flying-bird flying-bird   225 8月   7 23:14 helloworld.proto
$ 

这里是将proto文件生成对应的C++代码。下面的命令是生成Python代码:

$ protoc -I=. --python_out=. helloworld.proto
$ ls -l
total 44
-rw-rw-r-- 1 flying-bird flying-bird  2502 8月   7 23:16 helloworld_pb2.py
-rw-rw-r-- 1 flying-bird flying-bird 20490 8月   7 23:14 helloworld.pb.cc
-rw-rw-r-- 1 flying-bird flying-bird 10651 8月   7 23:14 helloworld.pb.h
-rw-rw-r-- 1 flying-bird flying-bird   225 8月   7 23:14 helloworld.proto
$ 

可以用protoc –help了解更多信息。

I'm sorry, as an AI language model, I don't have access to your local environment and cannot execute commands on your behalf. However, I can provide you with general instructions on how to run the protobuf compiler. To generate a file using the protobuf compiler, you need to follow these steps: 1. Install the protobuf compiler: You can download and install the protobuf compiler from the official website (https://developers.google.com/protocol-buffers/docs/downloads). 2. Define your message schema: You need to create a .proto file that defines the structure of your message. 3. Compile your message schema: Use the protobuf compiler to compile your .proto file. The command to compile a .proto file is: ``` protoc --proto_path=IMPORT_PATH --python_out=OUTPUT_DIR INPUT_FILE ``` Where: - `IMPORT_PATH`: The directory containing any .proto files that your schema imports. - `OUTPUT_DIR`: The directory where you want the generated Python files to be saved. - `INPUT_FILE`: The path to your .proto file. For example, if you have a file named `my_message.proto` in the current directory and you want to generate the Python code in a folder named `generated`, you can run: ``` protoc --proto_path=. --python_out=generated my_message.proto ``` 4. Use the generated code: Once you have generated the Python code, you can import it in your Python project and use it to serialize and deserialize your messages. The generated code will contain a Python class for each message type defined in your .proto file. I hope this helps! Let me know if you have any further questions.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值