protobuf安装/使用
原本是要在官网上下载的:
http://protobuf.googlecode.com/files/protobuf-2.5.0.tar.gz
可惜已被墙,幸好有好心人提供了以下地址:
http://pan.baidu.com/s/1pJlZubT
为了说明安装过程中文件的作用,我就指定目录安装了:
./configure --prefix=/usr/local/protobuf/
make
make check
make install
当然,安装前需要确保自己安装了gcc,g++,automake等,就不多说了。
安装完成之后,大概生成了这样一个目录结构
|-- bin
| `-- protoc
|-- include
| `-- google
| `-- protobuf
| |-- compiler
| | |-- code_generator.h
| | |-- command_line_interface.h
| | |-- cpp
| | | `-- cpp_generator.h
| | |-- importer.h
| | |-- java
| | | `-- java_generator.h
| | |-- parser.h
| | |-- plugin.h
| | |-- plugin.pb.h
| | |-- plugin.proto
| | `-- python
| | `-- python_generator.h
| |-- descriptor.h
| |-- descriptor.pb.h
| |-- descriptor.proto
| |-- descriptor_database.h
| |-- dynamic_message.h
| |-- extension_set.h
| |-- generated_enum_reflection.h
| |-- generated_message_reflection.h
| |-- generated_message_util.h
| |-- io
| | |-- coded_stream.h
| | |-- gzip_stream.h
| | |-- printer.h
| | |-- tokenizer.h
| | |-- zero_copy_stream.h
| | |-- zero_copy_stream_impl.h
| | `-- zero_copy_stream_impl_lite.h
| |-- message.h
| |-- message_lite.h
| |-- reflection_ops.h
| |-- repeated_field.h
| |-- service.h
| |-- stubs
| | |-- atomicops.h
| | |-- atomicops_internals_arm_gcc.h
| | |-- atomicops_internals_arm_qnx.h
| | |-- atomicops_internals_atomicword_compat.h
| | |-- atomicops_internals_macosx.h
| | |-- atomicops_internals_mips_gcc.h
| | |-- atomicops_internals_pnacl.h
| | |-- atomicops_internals_x86_gcc.h
| | |-- atomicops_internals_x86_msvc.h
| | |-- common.h
| | |-- once.h
| | |-- platform_macros.h
| | |-- template_util.h
| | `-- type_traits.h
| |-- text_format.h
| |-- unknown_field_set.h
| |-- wire_format.h
| |-- wire_format_lite.h
| `-- wire_format_lite_inl.h
`-- lib
|-- libprotobuf-lite.a
|-- libprotobuf-lite.la
|-- libprotobuf-lite.so -> libprotobuf-lite.so.8.0.0
|-- libprotobuf-lite.so.8 -> libprotobuf-lite.so.8.0.0
|-- libprotobuf-lite.so.8.0.0
|-- libprotobuf.a
|-- libprotobuf.la
|-- libprotobuf.so -> libprotobuf.so.8.0.0
|-- libprotobuf.so.8 -> libprotobuf.so.8.0.0
|-- libprotobuf.so.8.0.0
|-- libprotoc.a
|-- libprotoc.la
|-- libprotoc.so -> libprotoc.so.8.0.0
|-- libprotoc.so.8 -> libprotoc.so.8.0.0
|-- libprotoc.so.8.0.0
`-- pkgconfig
|-- protobuf-lite.pc
`-- protobuf.pc
然后我们先贴代码:
lm.helloworld.proto
package lm; message helloworld { required int32 id = 1; required string str = 2; optional int32 opt = 3; }
write.cc
#include<iostream> #include<fstream> #include<stdio.h> #include "lm.helloworld.pb.h" using namespace std; using namespace lm; int main() { lm::helloworld msg1; msg1.set_id(101); msg1.set_str("hello"); fstream output("./msg.pb", ios::out | ios::trunc | ios::binary); if(!msg1.SerializeToOstream(&output)) { cerr << "Failed to write msg."<<endl; return -1; } return 0; }
read.cc
#include<iostream> #include<fstream> #include<stdio.h> #include "lm.helloworld.pb.h" using namespace std; using namespace lm; void listmsg(const lm::helloworld &msg) { cout<<msg.id()<<endl; cout<<msg.str()<<endl; } int main() { lm::helloworld msg1; fstream input("./msg.pb", ios::in | ios::binary); if(!msg1.ParseFromIstream(&input)) { cerr << "Failed to write msg."<<endl; return -1; } listmsg(msg1); return 0; }
首先需要生成一个类似于lm.helloworld.proto的接口类的接口文件
/usr/local/protobuf/bin/protoc -I ./ --cpp_out=./ lm.helloworld.proto
我们可以看见生成了如下两个文件,这是编译的时候需要的
lm.helloworld.pb.cc
lm.helloworld.pb.h
而/usr/local/protobuf/bin/protoc正是protobuff用来生成此文件的程序;
然后我们就可以编译了
g++ -I /usr/local/protobuf/include/ lm.helloworld.pb.cc read.cc -o read `pkg-config --cflags --libs /usr/local/protobuf/lib/pkgconfig/protobuf.pc`
g++ -I /usr/local/protobuf/include/ lm.helloworld.pb.cc write.cc -o write `pkg-config --cflags --libs /usr/local/protobuf/lib/pkgconfig/protobuf.pc`
分别生成的write和read的可执行程序,就是我们使用protobuff的例子,先执行./write,后执行./read,就会看见结果:
[root@MYCR Protobuf]# ./read 101 hello
完