1.下載protobufios
#wget -c https://github.com/protocolbuffers/protobuf/releases/download/v3.7.1/protobuf-cpp-3.7.1.tar.gzgit
或者百度網盤下載protobuf3.7.1 提取碼: wfu5github
下載後,解壓文件protobuf-cpp-3.7.1.tar.gz,在解壓獲得的protobuf-3.7.1目錄下執行如下命令,--prefix指定安裝的根目錄app
$ ./configure --prefix=/usr/local測試
$ make spa
$ make check code
$ sudo make installblog
# ldconfigtoken
2. 測試ip
person.proto
syntax = "proto3"; package person; message Person{ string id=1; string name=2; string addr = 3; string test = 1000; } //protoc --cpp_out=. person.proto
/*! * Auth: xor * Date: 2019-6-9 * File: personWrite.cpp * Class: %{Cpp:License:ClassName} (if applicable) * Brief: * Note: */ #include "person.pb.h" #include <iostream> #include <fstream> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <string> #include <unistd.h> using namespace std; using namespace person; int main(int argc,char **argv){ person::Person person; person.set_id("111"); person.set_name("China"); person.set_addr("Aisa"); person.set_test("ttttt"); // serialize string output = ""; if(!person.SerializeToString(&output)) { perror("failed to write person.\n"); return -1; } cout << "output:" << output << endl; // serialize data to file fstream os("./person.info", ios::out | ios::trunc | ios::binary); if(!person.SerializeToOstream(&os)) { perror("failed to write person"); return -1; } printf("hello world!\n"); return 0; } // g++11 personWrite.cpp person.pb.cc -o xpW -lprotobuf -lpthread
#include "person.pb.h" #include <iostream> #include <fstream> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <string> #include <unistd.h> using namespace std; using namespace person; int main(int argc,char **argv){ person::Person person; int fd = open("./person.info", O_EXCL); assert(fd > 0); if(!person.ParseFromFileDescriptor(fd)) { perror("failed to parse msg"); return -1; } cout << "output:" << person.id() << " name:" << person.name() << " addr:" << person.addr() << " test:" << person.test() << endl; printf("hello world!\n"); return 0; } // g++11 personRead.cpp person.pb.cc -o xpR -lprotobuf -lpthread