PROJECT (Test)
SET(SRC_LIST hello.cpp)
ADD_EXECUTABLE(test ${SRC_LIST})
构建项目
cmake .
便会生成相应的Makefile文件 Protobuf
安装protobuf-c
需要先安装google protobuf
http://code.google.com/p/protobuf/downloads/list
./configure
make
make check
make install
下载
http://code.google.com/p/protobuf-c/downloads/list
./configure --prefix=$HOME/install
make
make install
A typical reason for this behaviour is a stale ld.so.cache; try to run ldconfig to update it after making sure that /usr/local/lib is listed in /etc/ld.so.conf.
make不过时可能需要执行 ldconfig 命令
To install into /usr like a normal package would, use --prefix=/usr
protobuf-c Simple complete example
protobuf-c works by taking a.proto file, and generating both .h and .c files for use in C programs.
amessage.proto
Generate .h and .c files from the command-line in your current working directory:
protoc-c --c_out=. amessage.proto
Serialize/pack Deserialize/upack the AMessage as follows:
amessage_demo.c
#include <stdio.h>
#include <stdlib.h>
#include "amessage.pb-c.h"
int main (int argc, const char * argv[])
{
AMessage msg = AMESSAGE__INIT; // AMessage
AMessage *rmsg;
void *buf; // Buffer to store serialized data
unsigned len; // Length of serialized data
if (argc != 2 && argc != 3)
{ // Allow one or two integers
fprintf(stderr,"usage: amessage a \n");
return 1;
}
msg.a = atoi(argv[1]); // Put an int in msg
if (argc == 3) { msg.has_b = 1; msg.b = atoi(argv[2]); } // Add another
len = amessage__get_packed_size(&msg); // This is calculated packing length
buf = malloc(len); // Allocated memory for packed/encode serialized data
amessage__pack(&msg,buf); // Put it in buf now
fprintf(stderr,"Writing %d serialized bytes\n",len); // See the length of message
fwrite(buf,len,1,stdout); // Write to stdout to allow direct command line piping
rmsg = amessage__unpack(NULL, len, buf);
printf("Received: a=%d", rmsg->a);
printf(" b=%d", rmsg->b);
printf("\n");
amessage__free_unpacked(rmsg, NULL);
free(buf); // Free the allocated serialized buffer
return 0;
}
编译:
gcc amessage_demo.c amessage.pb-c.c -o amessage_demo -lprotobuf-c
效果:
./amessage_demo 10 2
Writing: 4 serialized bytes
Received: a=10 b=2
安装protobuf-python环境:
cd protobuf目录下python目录
python setup.py install
Protobuf-c RPC service and Protobuf python client
首先写一个结构,重点是加上service这样会生成相应的service方法
rpc.proto