protobuf的安装
sudo apt-get install autoconf automake libtool curl make g++ unzip
git clone https://github.com/google/protobuf.git
cd protobuf
git submodule update --init --recursive
./autogen.sh
./configure
make
make check
sudo make install
sudo ldconfig # refresh shared library cache.
注意: 如果安装时报告 test失败,可以看一下log,如果是大块io测试失败,把虚拟机的内存调整到8G即可通过测试
protobuf的使用
-
利用
.proto
文件生成.c
/.cpp
和.h
文件protoc -I=./ --cpp_out=./out ./addressbook.proto
其中,
-I
指定的是.proto
文件的源目录,--cpp_out
指定的是C++文件的输出目录,./addressbook.proto
指定的是被编译的源文件 -
利用生成文件编译可执行文件
编译时要通过
pkg-config
来生成编译用的cflag
,不然可能会因为找错库文件而编译报错g++ ./src/addressbook.pb.cc ./src/pb_test.cpp -o pb_test -I./include `pkg-config --cflags --libs protobuf`