個人的なprotobuf-3.5.2実践:インストールとテスト
環境:CentOS 7
1、インストール
githubソースダウンロードアドレス:https://github.com/google/protobuf
chmod 777 -R protobuf-3.5.2 cd protobuf-3.5.2 ./autogen.sh ./configure make make install ldconfig #refresh shared library cache
Protoc--versionコマンドを実行してバージョンを表示します
firecats-MacBook-Pro:~ liuquandan$ which protoc
/usr/local/bin/protoc
firecats-MacBook-Pro:~ liuquandan$ protoc --version
libprotoc 3.5.2
通常、ProtoBufは/usr/localディレクトリにインストールされます.このディレクトリには、ProtoBufのヘッダファイル、静的ライブラリ、および動的ライブラリファイル/usr/local/bin/protoc/usr/local/include/google/protobuf/usr/local/lib/libprotobuf.*が含まれています.protoc:protobufが持参したコンパイルツール.protoファイル生成指定クラス–cpp_out:出力固有の言語とパスを指定する
2、demoテストを書く
helloworld.proto
person.proto
protoc -I=./--cpp_out=./helloworld.proto protoc -I=./--cpp_out=./person.proto
ファイルが自動的に生成されます.
helloworld.pb.h
helloworld.pb.cc
person.pb.h
person.pb.cc
CMakeLists.txt
main.cpp
コンパイルはパスしましたが、実行時にエラーが発生します:error while loading shared libraries:libprotobuf.so.15:cannot open shared object file:No such file or directoryこの場合/etc/ld.so.confにlibrdkafkaを加える.soが存在するディレクトリ:/usr/local/lib/そして端末でコマンドを実行して有効にする:[root@localhostetc]#ldconfig注意、/usr/local/lib/ライブラリファイルが更新されるたびに、端末がldconfigというコマンドを再実行する必要があります.
1、インストール
githubソースダウンロードアドレス:https://github.com/google/protobuf
chmod 777 -R protobuf-3.5.2 cd protobuf-3.5.2 ./autogen.sh ./configure make make install ldconfig #refresh shared library cache
Protoc--versionコマンドを実行してバージョンを表示します
firecats-MacBook-Pro:~ liuquandan$ which protoc
/usr/local/bin/protoc
firecats-MacBook-Pro:~ liuquandan$ protoc --version
libprotoc 3.5.2
通常、ProtoBufは/usr/localディレクトリにインストールされます.このディレクトリには、ProtoBufのヘッダファイル、静的ライブラリ、および動的ライブラリファイル/usr/local/bin/protoc/usr/local/include/google/protobuf/usr/local/lib/libprotobuf.*が含まれています.protoc:protobufが持参したコンパイルツール.protoファイル生成指定クラス–cpp_out:出力固有の言語とパスを指定する
2、demoテストを書く
helloworld.proto
syntax = "proto3";
package lm;
message helloworld
{
int32 id = 1; // ID
string str = 2; // str
}
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;
}
protoc -I=./--cpp_out=./helloworld.proto protoc -I=./--cpp_out=./person.proto
ファイルが自動的に生成されます.
helloworld.pb.h
helloworld.pb.cc
person.pb.h
person.pb.cc
CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
project(pbtest)
#set(SRC .)
# , DIR_SRCS
aux_source_directory(. DIR_SRCS)
add_executable(${PROJECT_NAME} ${DIR_SRCS})
TARGET_LINK_LIBRARIES(${PROJECT_NAME} protobuf)
main.cpp
#include
#include
#include
#include
#include
#include
#include
#include
#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!
";
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
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
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;
}
コンパイルはパスしましたが、実行時にエラーが発生します:error while loading shared libraries:libprotobuf.so.15:cannot open shared object file:No such file or directoryこの場合/etc/ld.so.confにlibrdkafkaを加える.soが存在するディレクトリ:/usr/local/lib/そして端末でコマンドを実行して有効にする:[root@localhostetc]#ldconfig注意、/usr/local/lib/ライブラリファイルが更新されるたびに、端末がldconfigというコマンドを再実行する必要があります.