一、下载Protocol Buffers
工程:GitHub - protocolbuffers/protobuf: Protocol Buffers - Google's data interchange format
以Protocol Buffers
v3.17.0为例:下载地址为Protocol Buffers v3.17.0
Release Protocol Buffers v3.17.0 · protocolbuffers/protobuf · GitHub
需要编译的是C++版本,可以选择protobuf-cpp-3.17.0.tar.gz
,也可以选择Source code (tar.gz)
,下面以Source code (tar.gz)
为例。下载后的文件为protobuf-3.17.0.tar.gz
。
1、PC端下载后的文件,如图:
2、rz上传至Linux机器你自己的目录下,如/home
二、编译与安装
在Linux机器上,进入到tar.gz包的目录下,执行如下步骤:
步骤 | 说明 | 指令 | 输出 |
1 | 压缩包解压 | tar -zxvf protobuf-3.17.0.tar.gz | ![]() |
2 | 进入编译目录cmake | cd protobuf-3.17.0 cd cmake/ | ![]() |
3 | 编译配置 | cmake . -Dprotobuf_BUILD_TESTS=OFF | (编译第一步) |
4 | 编辑 | cmake --build . | (编译第二步) |
5 | 指定目录安装 | make install DESTDIR=安装目录 ( ======= 如:指定目录安装:make install DESTDIR=/usr/local/protobuf-install 注意也可以默认安装:make install | (安装) |
备注: 指定安装目录和不指定的区别:当不指定目录安装时,protobuf将默认安装在Linux系统目录上:/usr/local 分别在bin、include、lib64的位置上。因此建议是指定目录安装,便于管理。
三、安装文件确认
在第二部分编译安装完成后,在安装目录下,可以看到以下三个文件夹bin(protoc可执行文件)、include(头文件)、lib64(库文件)
备注:你可任意拷贝以上三个目录至你想要的位置,用于测试编译。
详细内容见如下:
.
├── protobuf_tree
└── usr
└── local
├── bin
│ ├── protoc -> protoc-3.17.0.0
│ └── protoc-3.17.0.0
├── include
│ └── protobuf
│ ├── any.h
│ ├── any.pb.h
│ ├── any.proto
│ ├── api.pb.h
│ ├── api.proto
│ ├── arena.h
│ ├── arena_impl.h
│ ├── arenastring.h
│ ├── compiler
│ │ ├── code_generator.h
│ │ ├── command_line_interface.h
│ │ ├── cpp
│ │ │ └── cpp_generator.h
│ │ ├── csharp
│ │ │ ├── csharp_generator.h
│ │ │ └── csharp_names.h
│ │ ├── importer.h
│ │ ├── java
│ │ │ ├── java_generator.h
│ │ │ ├── java_kotlin_generator.h
│ │ │ └── java_names.h
│ │ ├── js
│ │ │ ├── js_generator.h
│ │ │ └── well_known_types_embed.h
│ │ ├── objectivec
│ │ │ ├── objectivec_generator.h
│ │ │ └── objectivec_helpers.h
│ │ ├── parser.h
│ │ ├── php
│ │ │ └── php_generator.h
│ │ ├── plugin.h
│ │ ├── plugin.pb.h
│ │ ├── plugin.proto
│ │ ├── python
│ │ │ └── python_generator.h
│ │ └── ruby
│ │ └── ruby_generator.h
│ ├── descriptor_database.h
│ ├── descriptor.h
│ ├── descriptor.pb.h
│ ├── descriptor.proto
│ ├── duration.pb.h
│ ├── duration.proto
│ ├── dynamic_message.h
│ ├── empty.pb.h
│ ├── empty.proto
│ ├── extension_set.h
│ ├── extension_set_inl.h
│ ├── field_mask.pb.h
│ ├── field_mask.proto
│ ├── generated_enum_reflection.h
│ ├── generated_enum_util.h
│ ├── generated_message_reflection.h
│ ├── generated_message_table_driven.h
│ ├── generated_message_util.h
│ ├── has_bits.h
│ ├── implicit_weak_message.h
│ ├── io
│ │ ├── coded_stream.h
│ │ ├── gzip_stream.h
│ │ ├── io_win32.h
│ │ ├── printer.h
│ │ ├── strtod.h
│ │ ├── tokenizer.h
│ │ ├── zero_copy_stream.h
│ │ ├── zero_copy_stream_impl.h
│ │ └── zero_copy_stream_impl_lite.h
│ ├── map_entry.h
│ ├── map_entry_lite.h
│ ├── map_field.h
│ ├── map_field_inl.h
│ ├── map_field_lite.h
│ ├── map.h
│ ├── map_type_handler.h
│ ├── message.h
│ ├── message_lite.h
│ ├── metadata.h
│ ├── metadata_lite.h
│ ├── parse_context.h
│ ├── port_def.inc
│ ├── port.h
│ ├── port_undef.inc
│ ├── reflection.h
│ ├── reflection_ops.h
│ ├── repeated_field.h
│ ├── service.h
│ ├── source_context.pb.h
│ ├── source_context.proto
│ ├── struct.pb.h
│ ├── struct.proto
│ ├── stubs
│ │ ├── bytestream.h
│ │ ├── callback.h
│ │ ├── casts.h
│ │ ├── common.h
│ │ ├── hash.h
│ │ ├── logging.h
│ │ ├── macros.h
│ │ ├── map_util.h
│ │ ├── mutex.h
│ │ ├── once.h
│ │ ├── platform_macros.h
│ │ ├── port.h
│ │ ├── status.h
│ │ ├── stl_util.h
│ │ ├── stringpiece.h
│ │ ├── strutil.h
│ │ └── template_util.h
│ ├── text_format.h
│ ├── timestamp.pb.h
│ ├── timestamp.proto
│ ├── type.pb.h
│ ├── type.proto
│ ├── unknown_field_set.h
│ ├── util
│ │ ├── delimited_message_util.h
│ │ ├── field_comparator.h
│ │ ├── field_mask_util.h
│ │ ├── json_util.h
│ │ ├── message_differencer.h
│ │ ├── time_util.h
│ │ ├── type_resolver.h
│ │ └── type_resolver_util.h
│ ├── wire_format.h
│ ├── wire_format_lite.h
│ ├── wrappers.pb.h
│ └── wrappers.proto
└── lib64
├── cmake
│ └── protobuf
│ ├── protobuf-config.cmake
│ ├── protobuf-config-version.cmake
│ ├── protobuf-module.cmake
│ ├── protobuf-options.cmake
│ ├── protobuf-targets.cmake
│ └── protobuf-targets-noconfig.cmake
├── libprotobuf.a
├── libprotobuf-lite.a
├── libprotoc.a
└── pkgconfig
├── protobuf-lite.pc
└── protobuf.pc
四、测试proto生成c++文件
1、在以上目录,这里简单演示,在安装目录/protobuf/usr/local/bin下创建test.proto文件;
syntax = "proto3"; // 使用proto3语法
message SearchRequest {
string query = 1;
int32 page_number = 2;
int32 result_per_page = 3;
}
2、然后使用bin目录中的protoc生成test.pb.h和test.pb.cc文件,命令如下(–cpp_out指定生成文件位置):这里用【.】表示生成的到当前的目录位置:
./protoc test.proto --cpp_out=.
执行后生成的c++文件如下,看到pb.cc和pb.h表示已生成成功
3、写一个测试程序demo,代码如下:
#include <iostream>
#include "test.pb.h"
int main()
{
SearchRequest request;
request.set_query("hello, world. this is the protobuf usage");
std::cout << request.query() << std::endl;
return 0;
}
4、编译main文件
g++ -o main main.cpp test.pb.cc -std=c++11 -I ../include/ -L ../lib64/ -lprotobuf
备注:1、以上的-I ../include/ -L ../lib64/ 均为protobuf的安装目录。
2、编译时如果不指定-o ,则默认生成a.out文件,也是可以执行的:
小知识: a.out的由来_楚楚可薇的博客-优快云博客_aout
5、执行main文件,输出结果:
至此,protobuf的安装和c++的使用方式已测试完成可用。