一、X86 ubuntu平台

1.下载protobuf-c ,下载最新版本就行

下载地址:https:///protobuf-c/protobuf-c/tags

2.编译与安装

安装依赖库

sudo apt-get install autoconf automake libtool  curl make g++ unzip pkg-config

安装protobuf3(要先安装protobuf2.6.1以上的版本后,才能正常编译出protobuf-c的bin、lib等)

下载地址:https:///protocolbuffers/protobuf/tags

解压后,进入解压目录,执行以下命令(可以看下src/README.md的说明)

  git submodule update --init --recursive(如果不是从git仓库clone下来的源码,可忽略)
  ./autogen.sh(如果不是从git仓库clone下来的源码,可忽略)

  ./configure
  make
  make check
  sudo make install
  sudo ldconfig # refresh shared library cache.

编译protobuf-c

    ./autogen.sh (如果不是从git仓库clone下来的源码,可忽略)

  ./configure --prefix=xxx/xxx/protobuf-c-x86(--prefix选一个自定义的目录,可以不加这选项,默认安装的系统文件夹)

   make

   make install

至此,protobuf-c安装完成。

3.protobuf-c的使用

编写.proto文件,如test.proto

syntax="proto2";
message TestMessage{
    optional  uint64 id=1;
    repeated  uint32 state=2;
    required   string name=3;
}

message AllMessage{
    required bytes data;
   required  uint64 all_id=1;
   required  uint32 all_state=2;
   required TestMessage testmsg=3;     
}

  字段规则类型:

  required:表示后面的数据是必须的。

  optional:表示后面数据是可选的。

  repeated:表示后面的数据是一个数组

protobuf-c 纯C版本的protobuf移植与使用_git仓库

 

  生成.pb-c.c和.pb-c.h文件

  可以将.proto文件复制到安装目录(xxx/xxx/protobuf-c-x86/bin),即proto-c可执行文件所在目录,终端执行

  ./protoc-c -I=. --c_out=. ./test.proto

  可以生成test.pb-c.c和test.pb-c.h文件

  如果安装在系统文件夹

  可以在test.proto所在的文件夹,终端执行

  protoc-c -I=. --c_out=. ./test.proto

使用.pb-c.c和.pb-c.h文件