Environment: CentOS7

 

1, install,

Github source code download address: github.com/google/prot…

Chmod 777-r protobuf-3.5.2 CD protobuf-3.5.2./autogen.sh./configure make make install ldconfig #refresh shared library cache

 

Run the protoc –version command to view the version

firecats-MacBook-Pro:~ liuquandan$ which protoc

/usr/local/bin/protoc

firecats-MacBook-Pro:~ liuquandan$ protoc –version

Libprotoc 3.5.2

Typically, ProtoBuf is installed in /usr/local, which contains the ProtoBuf header file, Static library and dynamic library file/usr/local/bin/protoc/usr/local/include/Google/protobuf/usr/local/lib/libprotobuf. * protoc: Protobuf builds a. Proto file that generates a specified class – cpp_out: specifies the output language and path

 

 

 

2. Write demo tests

helloworld.proto

syntax = "proto3";

package lm; 
message helloworld 
{ 
    int32     id = 1;  // ID 
    string    str = 2;  // str 
}
Copy the code

person.proto

syntax="proto3";
package tutorial;

message Person
{
	string name = 1;
	int32 id = 2;
	string email = 3;

	enum PhoneType
	{
		MOBILE = 0;
		HOME = 1;
		WORK = 2;
	}

	message PhoneNumber
	{
		string number = 1;
		PhoneType type = 2; 
	}

	repeated PhoneNumber phone = 4;
}

message AddressBook
{
	repeated Person person =1;
}
Copy the code

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

protoc -I=./ –cpp_out=./ person.proto

 

Files will be generated automatically:

helloworld.pb.h

helloworld.pb.cc

person.pb.h

person.pb.cc

 

CMakeLists.txt

Cmake_minimum_required (VERSION 2.8) Project (pbtest) #set(SRC.) Aux_source_directory (.dir_srcs) add_executable(${PROJECT_NAME} ${DIR_SRCS}) TARGET_LINK_LIBRARIES(${PROJECT_NAME} protobuf)Copy the code

 

main.cpp

#include <iostream>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string>
#include <pthread.h>
#include <fstream>#include "helloworld.pb.h" using namespace std; #define BUFFSIZE 1024 int main() { GOOGLE_PROTOBUF_VERIFY_VERSION; //eg1, write file lm::helloworld msg1; msg1.set_id(1001); msg1.set_str("hello world"); fstream output("./log", ios::out | ios::trunc | ios::binary); if (! msg1.SerializeToOstream(&output)) { cerr << "Failed to write msg." << endl; return -1; } output.close(); cout << msg1.id() << endl; cout << msg1.str() << endl; //eg2, read file lm::helloworld msg2; fstream input("./log", ios::in | ios::binary); if (! input) { cerr << "open file failed! \n"; return -1; } if (! msg2.ParseFromIstream(&input)) { cerr << "Parse file failed!" << endl; return -1; } input.close(); cout << msg2.id() << endl; cout << msg2.str() << endl; //eg3, write buf, protobuf serialize lm::helloworld msg3; msg3.set_id(1002); msg3.set_str("good idea"); char buf[BUFFSIZE]; memset(buf, 0, BUFFSIZE); msg3.SerializeToArray(buf, BUFFSIZE); //eg4, read buf, protobuf deserialize lm::helloworld msg4; msg4.ParseFromArray(buf, BUFFSIZE); cout << msg4.id() << endl; cout << msg4.str() << endl; // Optional: Delete all global objects allocated by libprotobuf. google::protobuf::ShutdownProtobufLibrary(); return 0; }Copy the code

 

 

Error while loading shared libraries: libprotobuf.so.15: Cannot open shared object file: No such file or directory Add the directory where librdkafka.so resides to /etc/ld.so.conf :/usr/local/lib/ Run the following command on the terminal to make the librdkafka.so take effect: [root@localhost etc]# ldconfig note that /usr/local/lib/ldconfig requires the terminal to run the ldconfig command again every time the library file is updated.