一、安裝依賴包
$sudo apt-get install build-essential
二、安裝Protobuf
1、下載Protobuf,我使用的是Protobuf2.6.1
$ wget' target='_blank'>https://github.com/google/protobuf/releases/download/v2.6.1/protobuf-2.6.1.tar.gz[/code] 2、解壓
$ sudo chmod 777 protobuf-2.6.1.tar.gz
$ tar -vxzf protobuf-2.6.1.tar.gz
3、安裝
$ cd protobuf-2.6.1/
$ ./configure
$ make
$ sudo make install
4、添加環境變量
vim ~/.profile
加入:export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib
保存後退出,編譯source ~/.profile
5、測試
protoc --version
三、安裝nanopb
1、下載
wget' target='_blank'>http://koti.kapsi.fi/~jpa/nanopb/download/nanopb-0.3.5-linux-x86.tar.gz[/code]
2、解壓
$ tar -zxvf nanopb-0.3.5-linux-x86.tar.gz
3、復制
$ sudo cp nanopb-0.3.5-linux-x86 /opt/
四、生成文件
1、制作proto文件,Message.proto
message Person{
required int32 age = 1;
required string name = 2;
}
2、制作option文件,Message.options
Person.name max_size:16
3、生成C語言文件
$ /opt/nanopb-0.3.5-linux-x86/generator-bin/protoc --nanopb_out=. Message.proto
4、生成java語言文件
protoc --java_out=. Message.proto
五、測試代碼
1、將解壓的nanopb-0.3.5-linux-86文件夾下的pb.h, pb_common.c, pb_common.h, pb_decode.c, pb_decode.h, pb_encode.c, pb_encode.h復制到proto文件目錄下。
2、新建main.c,內容如下:
#include "Message.pb.h"
#include "pb.h"
#include "pb_encode.h"
#include "pb_decode.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
int main()
{
/* This is the buffer where we will store our message. */
uint8_t buffer[128];
size_t message_length;
bool status;
uint8_t i;
/* Encode our message */
{
/* Allocate space on the stack to store the message data.
*
* Nanopb generates simple struct definitions for all the messages.
* - check out the contents of simple.pb.h!
* It is a good idea to always initialize your structures
* so that you do not have garbage data from RAM in there.
*/
Person person = Person_init_zero;
memset(buffer, 0, 128);
/* Create a stream that will write to our buffer. */
pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
person.age = 20;
strcpy(&person.name, "zhujun");
/* Now we are ready to encode the message! */
status = pb_encode(&stream, Person_fields, &person);
message_length = stream.bytes_written;
printf("encode ok\r\n");
for (i = 0; i < message_length; i++)
{
printf("%d,", buffer[i]);
}
printf("\r\n");
/* Then just check for any errors.. */
if (!status)
{
printf("Encoding failed: %s\n", PB_GET_ERROR(&stream));
return 1;
}
}
/* Now we could transmit the message over network, store it in a file or
* wrap it to a pigeon's leg.
*/
/* But because we are lazy, we will just decode it immediately. */
{
/* Allocate space for the decoded message. */
Person person = Person_init_zero;
/* Create a stream that reads from the buffer. */
pb_istream_t stream = pb_istream_from_buffer(buffer, message_length);
/* Now we are ready to decode the message. */
status = pb_decode(&stream, Person_fields, &person);
/* Check for errors... */
if (!status)
{
printf("Decoding failed: %s\n", PB_GET_ERROR(&stream));
return -1;
}
/* Print the data contained in the message. */
printf("age was [%d].\n", person.age);
printf("name was [%s].\n", person.name);
}
return 0;
}
六、問題
1、C在使用string類型的時候,必須通過options,限定string的長度,否則運行將出錯。
2、nanopb可通過python生成.c和.h文件,但我沒有成功,不知道缺少了哪個庫。