企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[TOC] ### ProtoBuf下载 ***** github地址:[https://github.com/google/protobuf](https://github.com/google/protobuf) ### docker安装ProtoBuf ***** Dockerfile文件: ``` FROM centos:7 RUN yum install -y net-tools \ libgomp.x86_64 \ libtool-ltdl-devel.x86_64 \ vim \ psmisc.x86_64 \ libuuid-devel.x86_64 \ zlib-devel.x86_64 \ unixODBC-devel.x86_64 \ mariadb-devel.x86_64 \ dos2unix \ cmake \ make \ gcc-4.8.5 \ gcc-c++-4.8.5 \ autoconf \ automake \ libtool \ git WORKDIR /home/project RUN git clone https://github.com/google/protobuf \ && cd protobuf \ && chmod +x -R ./* \ && ./autogen.sh && ./configure && make && make check && make install CMD /bin/bash ``` ### Linux/MacOs ***** ProtoBuf在linux上编译 进入 ProtoBuf目录 ``` $ cd protobuf ``` 执行`autogen.sh` 脚本,生成`configure`文件 ``` $ ./autogen.sh ``` 执行命令: ``` $ ./configure && make && make check && make install ``` ### windows平台上 ***** ProtoBuf的编译 ### `proto`文件在代码中的使用 #### 在C++中的使用 编写`.proto`定义文件 `message.proto`: ``` syntax = "proto2"; package tutorial; //在代码中作为命名空间 message Person { required string name = 1; required int32 id = 2; // Unique ID number for this person. optional string email = 3; enum PhoneType { MOBILE = 0; HOME = 1; WORK = 2; } message PhoneNumber { required string number = 1; optional PhoneType type = 2 [default = HOME]; } repeated PhoneNumber phone = 4; } // Our address book file is just one of these. message AddressBook { repeated Person person = 1; } ``` `.proto`文件关键性地方分析: ``` syntax = "proto2"; 作为版本号 package tutorial; 在代码中作为命名空间 message是定义消息的关键字,同比c++中的struct/class,message可以嵌套使用 限定符required表示该字段必须存在,否则对应的message“未初始化”,debug模式下导致断言,release模式下解析失败; 限定符optional表示该字段可选存在, 限定符repeated表示该字段为重复字段,可以看着动态数组 int32,string 为protobuf数据类型 文件注释使用 // enum 为protobuf定义union类型属性 ``` 编译`.proto`文件 ``` proto --cpp_out=./ message.proto //注意 --cpp_out后有两个参数./表示输出目录 message.proto表示要编译的proto文件路径 ``` 生成 `message.pb.h`跟`message.pb.cc`文件 c++中使用这两个文件 ``` #include <iostream> #include "message.pb.h" #include <fstream> using namespace std; using namespace tutorial; int main() { // /*放置消息*/ // Person person; // person.set_name("zane"); // person.set_id(1234); // person.set_email("1001@qq.com"); // fstream output("file",ios::out | ios::binary); // person.SerializeToOstream(&output); /*读取消息*/ Person readPerson; fstream input("file", ios::in | ios::binary); readPerson.ParseFromIstream(&input); std::cout << "Name:" << readPerson.name() << std::endl; std::cout << "E-Mail:" << readPerson.email() << std::endl; return 0; } ``` 文件目录 ``` |— TestProtoBuf |— bin |— inc |- message.pb.h |— src |- main.cpp |- message.pb.cc |— protoc |— message.proto |— CMakeLists.txt ``` CMakeLists.txt编写 ``` cmake_minimum_required(VERSION 2.8) project(IM_Protobuf) set(PROJECT_OUTNAME HelloProtoBuf) SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -O2 -fshort-wchar -fopenmp -fPIC") SET(PROJECT_LIBS_PLATFORM libprotobuf.so ) SET(PROJECT_HDRS inc/message.pb.h ) INCLUDE_DIRECTORIES( inc/ ) SET(PROJECT_SRCS src/main.cpp src/message.pb.cc ) # 二进制文件输出目录 SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin/) add_executable(${PROJECT_OUTNAME} ${PROJECT_SRCS} ${PROJECT_HDRS}) TARGET_LINK_LIBRARIES(${PROJECT_OUTNAME} ${PROJECT_LIBS_PLATFORM}) ```