从github上获取
git clone https://github.com/protocolbuffers/protobuf.git
执行以下命令安装protobuf
./autogen.sh
./configure
make
make install
lm.helloworld.proto里面内容
syntax = "proto2";
package lm;
message helloworld
{
required int32 id=1;
required string str=2;
optional int32 opt=3;
}
编译
protoc -I=./ --cpp_out=./ ./lm.helloworld.proto
编写测试程序
#include "lm.helloworld.pb.h"
#include <iostream>
#include <fstream>
#include <string>
int main(void)
{
lm::helloworld msg1;
msg1.set_id(101);
msg1.set_str("hello");
std::string filename = "single_length_delimited_all_example1_val_result";
std::fstream output(filename, std::ios::out | std::ios::trunc | std::ios::binary);
if (!msg1.SerializeToOstream(&output))
{
std::cerr << "Failed to write example1." << std::endl;
exit(-1);
}
return 0;
}
编译测试程序
g++ lm.helloworld.writer.cpp lm.helloworld.pb.cc -o writer `pkg-config --cflags --libs protobuf`
执行测试程序
export LD_LIBRARY_PATH=":/usr/local/lib/"
./writer