protobuf在嵌入式linux下的移植及c语言调用

关于什么是protobuf,网上搜搜一大堆,很多人用的都还是json,以为json是多种语言传输数据是万能的,看完了protobuf的实现,就明白了简单高效才是王道。

简单介绍:

protobuf很出名,是google开发的序列化库,很多公司都使用它作为接口的数据结构。地址:https://developers.google.com/protocol-buffers/ 
支持java、c++、go等多种语言,几乎所有主流语言都支持。

先在电脑上安装,随后交叉编译,

网上的教程大多是基于2.x的版本的,现在想用下最新的, 就编译了下3.6版本。编译环境Ubutu 14.04

官网下载:https://github.com/google/protobuf

https://github.com/google/protobuf/releases

protobuf-all-3.6.0.tar.gz

官网安装手册:https://github.com/google/protobuf/blob/master/src/README.md

sudo apt-get install 

autoconf automake libtool curl make g++ unzip

cd protobuf-3.6.0/

./configure --prefix=/usr/local/protobuf
make
make check
make install
protoc --version
cd /usr/local/protobuf/lib/pkgconfig
export PATH=$PATH:/usr/local/protobuf/bin/
export PKG_CONFIG_PATH=/usr/local/protobuf/lib/pkgconfig/
source /etc/profile

cd /etc/ld.so.conf.d
vi libprotobuf.conf  (/usr/local/protobuf/lib/pkgconfig/)
ldconfig

下面开始交叉编译:

export PATH=$PATH:/home/yang/b503/ctools/gcc-linaro-arm-linux-gnueabihf-4.9-2014.09_linux/bin
先把交叉编译链添加到当前的环境变量里,

然后配置configure,

我的配置如下:

CC=arm-linux-gnueabihf-gcc CXX=arm-linux-gnueabihf-g++ ./configure --prefix=/home/linux/arm/protobuf --host=arm-linux
注意:CXX一定要配置为-g++而非-gcc

--host这项也一定要指定,才是交叉编译

最后,make, make install

编译完毕,没有报错。

protobuf很出名,是google开发的序列化库,很多公司都使用它作为接口的数据结构。地址:https://developers.google.com/protocol-buffers/ 
支持java、c++、go等多种语言,几乎所有主流语言都支持,但是官方没给出c语言的支持。经过一番寻找,找到了protobuf-c。在github中有很多protobuf c版本的实现,不过个人感觉还是这个protobuf-c更直接。其github地址:https://github.com/protobuf-c/protobuf-c 。 

下面总结下protobuf-c 的编译和移植过程及遇到的问题:

首先版本要匹配,porotobuf-c 目前最新版本是1.30.需要依赖protobufcpp3,0以上的版本。以protobufcpp3.4为例。

在构建protobuf-c之前,我们需要安装一些依赖库:autoconf、automake、protobuffer、libtool,当然也需要一些基本的系统依赖(如:gcc-c++等)。autoconf、automake和libtool可以直接使用apt-get、yum、brew等安装。protobuffer虽然也可以使用libprotobuf,不过还是推荐源码安装。

构建protobuf-c

注意如果在构建protobuf-c之前没有安装protobuf的话,生成protobuf-c的configure文件肯定会报如下错误!!

configure: error: Package requirements (protobuf >= 2.6.0) were not 
met:No package ‘protobuf’ found

需要先下载protobufcpp 1.34,地址在:

https://github.com/google/protobuf/releases

先编译protobufcpp1.34。

执行./configure了,但是根目录下居然没有configure文件,却有一个autogen.sh,原来是因为protobuf的编译方式做了修改,要执行autogen.sh才会生成configure脚本。

但在执行autogen.sh时出错了,因为google.com被墙了,我的虚拟机里无法下载gtest,于是手动下载googletest-release-1.5.0.zip,解压缩后,改名为gtest放在protobuf-1.34目录下. 

构建protobuf-c其实跟protobuffer几乎没任何区别,也是通过autogen.sh脚本生成configure配置脚本,然后执行make和install。但是需要注意的是protobuf-c在构建的时候不会自动把相关头文件拷贝到/usr/include下,需要手动拷贝。

https://github.com/protobuf-c/protobuf-c.git 克隆下protobuf-c源码。

git clone https://github.com/protobuf-c/protobuf-c.git

与构建protobuffer一样,分别执行:

./autogen.sh
  •  

接下来 make,make install

最后把头文件拷贝到/usr/include下

sudo cp -r /usr/local/protobuf-c/include/protobuf-c /usr/include 

按照网上教程,应该执行./configure了,但是根目录下居然没有configure文件,却有一个autogen.sh,原来是因为protobuf的编译方式做了修改,要执行autogen.sh才会生成configure脚本。

但在执行autogen.sh时出错了,因为google.com被墙了,我的虚拟机里无法下载gtest,于是手动下载googletest-release-1.5.0.zip,解压缩后,改名为gtest放在protobuf目录下 

configure: error: required protobuf header file not found

export PKG_CONFIG_PATH=/home/linux/arm/protobuf/lib/pkgconfig/

export PKG_CONFIG_PATH=/usr/local/protobuf3.4/lib/pkgconfig/

 

./configure --prefix=/usr/local/protobuf-c

protobuf-c 1.3.0

        CC:                     gcc -std=gnu99

        CFLAGS:                 -g -O2

        CXX:                    g++

        CXXFLAGS:               -g -O2

        LDFLAGS:                

        LIBS:                   

        prefix:                 /usr/local/protobuf-c

        sysconfdir:             ${prefix}/etc

        libdir:                 ${exec_prefix}/lib

        includedir:             ${prefix}/include

        pkgconfigdir:           ${libdir}/pkgconfig

        bigendian:              no

        protobuf version:       libprotoc 3.4.0

make ,make install 

root@yang-vir:/usr/local/protobuf-c/bin# ./protoc-c --version

protobuf-c 1.3.0

libprotoc 3.4.0

 

接下来再交叉编译:

 1、打包下载protobuf-c,地址:https://github.com/protobuf-c/protobuf-c

       2、解压后得到protobuf-c-master文件夹,进入该文件夹执行如下命令

       3、./autogen.sh

       4、CC=arm-linux-gnueabi-gcc CXX=g++ ./configure --host=arm-linux-gnueabi

       5、make

       6、等待完成,最终会在protobuf-c-master /protobuf-c/.libs目录下生成.so动态库

在给protobuf-c进行环境配置的时候,查找不到上一步所安装protobuf的库文件,而这些库文件又是通过pkgconfig配置进行查找的。 该配置文件此时是在/usr/local/lib/pkgconfig/下 ,也就是问题中所提示的:Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.  。所以我们只需要将PKG_CONFIG_PATH 指定到/usr/local/lib/pkgconfig/即可

在交叉编译protobufcpp3.4时会报个错,解决办法如下:

I just send #3934 to fix the issue. You can just edit the src/Makefile.am locally and remove the "BUILT_SOURCES = ..." line. After that "make" should work.

 

CC=arm-linux-gnueabihf-gcc CXX=arm-linux-gnueabihf-g++ ./configure --prefix=/home/linux/arm/protobuf-c --host=arm-linux

export PKG_CONFIG_PATH=/home/linux/arm/protobuf3.41/lib/pkgconfig/

 

附带一个使用示例:

1、首先写一个.proto扩展名的文件test.proto,内容格式如下

message test

{

    required uint32 test_1=1;

    required string test_2=2;

    required string test_3=3;

    required string test_4=4;   

}

2、执行命令生成.c和.h文件,protoc-c可执行文件已经预先生成了。

./protoc-c test.proto

3、写示例代码main.c:

解析:protobuf的核心就是pack和unpack以及最后的free_unpack,比如客户端要给服务端发送一个msg,客户端把pack好的pBuf缓冲区数据直接发出去,服务端收到数据之后,再从pBuf里面“取出”out结构体指针指向的msg,最后调用free_unpacked释放out就可以了。

//main.c
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
 
#include "test.pb-c.h"
 
int main(void)
{
    Test msg,*out;
    unsigned char *pBuf;
    unsigned int Len;
    test__init(&msg);            //init default
 
    msg.test_1=1;
    msg.test_2="5149013220584027";
    msg.test_3="5149013108519750";
    msg.test_4="121212121";
 
    Len=test__get_packed_size(&msg);
 
    printf("msg pack size %d\n",Len);
 
    pBuf=malloc(Len);
    if(pBuf)
    {
        test__pack(&msg,pBuf);        //construct msg to pBuf
        FILE *fp =fopen("protobuf.txt","wb+");        //output raw
        if(fp)
        {
            fwrite(pBuf,Len,1,fp);
            fclose(fp);
            fp=NULL;
        }
 
        out=test__unpack(NULL,Len,pBuf);
        if(out)
        {
            printf("out->test_1=%d\n",((Test*)out)->test_1);
            printf("out->test_2=%s\n",((Test*)out)->test_2);
            printf("out->test_3=%s\n",((Test*)out)->test_3);
            printf("out->test_4=%s\n",((Test*)out)->test_4);
            test__free_unpacked(out,NULL);
        }
    }
 
    printf("End\n");
}
 

4、编译,最终生成main可执行文件,将main放在开发板上,注意,相关的libc库和libprotobuf-c.so链接库都放在了开发板上/usr/arm-linux-gnueabi/lib目录下,否则运行是找不到.so库的

arm-linux-gnueabi-gcc main.c -o main test.pb-c.c -I./ -lprotobuf-c -L./protobuf-c-arm/lib -Wl,-rpath,/usr/arm-linux-gnueabi/lib

5、运行./main

实现了msg消息发送给out输出,同样类型的消息,Json需要91个字节,protobuf只需要49个字节,其实json里面的键值对name是完全不需要传输的,因为通讯双方都知道对应的name,只需要传输的只是value,但是每次传输数据,都要把name都传来传去,消耗了大量的带宽和数据存储空间。

export PKG_CONFIG_PATH=/home/linux/arm/protobuf3.41/lib/pkgconfig/

arm-linux-gnueabihf-gcc main.c -o main test.pb-c.c -I./ -lprotobuf-c -L./lib

root@b503_lcd:/app/city_app/opt# ls

enGPIO.sh           libprotobuf-c.so.1  restart.sh~         setconfig.sh        test.out

libprotobuf-c.so    main                rz                  setconfig.sh~

root@b503_lcd:/app/city_app/opt# ./main

msg pack size 49

out->test_1=1

out->test_2=5149013220584027

out->test_3=5149013108519750

out->test_4=121212121

End


附录:

1、经过测试,我们发现,其实protobuf的原理很简单,和两端都是C语言实现的client/server直接传输结构体变量原理是一样的,我们都知道,C语言结构体成员的存储方式都是顺序存储。所以发送和接收方都按照对应的成员排列位置进行解析,就可以实现数据的传输。

2、但是protobuf设计初衷应该是为了适应不同的语言之间数据传输,像java写的server里面就没有结构体,所以就不能传输C写的client里面的结构体变量给对方,对方是解析不了的。另外protobuf在.proto文件里面指定了具体的位置编号,否则应该就没办法生成.c和.h文件,如果后续双方通讯格式要做调整,双方都使用同一个修改后的.proto文件重新生产对应的源文件,重新编译即可。

3、为什么说用protobuf比json简单高效,原因就是你用json传输数据,每次字符串里面都传输了一堆没用的数据,比如键值对的冒号,以及键值名字和值的双引号,还有大小括号,因为通讯双方都知道对应的键值名以及怎么解析json(如果不知道键值名字,收到的数据还怎么解析?)。

双方都有的数据还每次传来传去,不是浪费是什么? 纯粹就是浪费带宽和存储,要传输的对方没有的数据,这才是有用数据。有人就说,这几个括号和键值名字能浪费多少带宽和空间,别小看这些小东西,假如数据交互量大和非常频繁,你就知道了,这里面的存储空间和带宽消耗差距是可怕惊人的。
测试结果如下:

yang@yang-vir:/mnt/hgfs/b503/new/protoc$ make
Building app...
arm-poky-linux-gnueabi-gcc  -march=armv7ve -marm -mfpu=neon  -mfloat-abi=hard -mcpu=cortex-a7 --sysroot=/opt/myir-imx-fb/4.1.15-2.0.1/sysroots/cortexa7hf-neon-poky-linux-gnueabi -c -g -Wall -I./ test.pb-c.c -o  test.pb-c.o    
arm-poky-linux-gnueabi-gcc  -march=armv7ve -marm -mfpu=neon  -mfloat-abi=hard -mcpu=cortex-a7 --sysroot=/opt/myir-imx-fb/4.1.15-2.0.1/sysroots/cortexa7hf-neon-poky-linux-gnueabi -c -g -Wall -I./ main.c -o  main.o    
Generating ...
arm-poky-linux-gnueabi-gcc  -march=armv7ve -marm -mfpu=neon  -mfloat-abi=hard -mcpu=cortex-a7 --sysroot=/opt/myir-imx-fb/4.1.15-2.0.1/sysroots/cortexa7hf-neon-poky-linux-gnueabi -o test_protoc ./test.pb-c.o ./main.o -L ./lib -g   -lprotobuf-c 
OK!
yang@yang-vir:/mnt/hgfs/b503/new/protoc$ ^C
yang@yang-vir:/mnt/hgfs/b503/new/protoc$ ls
test.pb-c.c  test.pb-c.o  lib   main.c  makefile    test_protoc
test.pb-c.h test.proto   main  main.o  protobuf-c
yang@yang-vir:/mnt/hgfs/b503/new/protoc$

arm-linux-gnueabihf-gcc main.c -o main test.pb-c.c -I./ -lprotobuf-c -L./lib

 

root@b503_lcd:/app/city_app/opt# ls

enGPIO.sh           libprotobuf-c.so.1  restart.sh~         setconfig.sh        test.out

libprotobuf-c.so    main                rz                  setconfig.sh~

root@b503_lcd:/app/city_app/opt# ./main

msg pack size 49

out->test_1=1

out->test_2=5149013220584027

out->test_3=5149013108519750

out->test_4=121212121

End

 

测试demo:

########################################
#makefile
########################################
#-rdynamic -pthread -lrt -lasound -lucgui -lpicc -licc -lmenures1 -LzfbAuth -lposoffline   -lhw_b503
#编译主程序
BINARY  := test_protoc
OBJ_DIR := ./
#CC= gcc
#LD= ld
CFLAGS=  -g -Wall -I./
LDSCRIPT=   -g   -lprotobuf-c
LDFLAGS= -L ./lib

SRC  = $(wildcard *.c)
DIR  = $(notdir $(SRC))
OBJS = $(patsubst %.c,$(OBJ_DIR)%.o,$(DIR))

.PHONY: clean 
all:  prebuild  $(BINARY)

prebuild:
    @echo Building app...

$(BINARY) : $(OBJS)
    @echo Generating ...
    $(CC) -o $(BINARY) $(OBJS) $(LDFLAGS) $(LDSCRIPT) 
    @echo OK!

$(OBJ_DIR)%.o : %.c
    $(CC) -c $(CFLAGS) $< -o  $@    
clean:
    rm -f $(OBJ_DIR)*.o
    @echo Removed!
 

  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

特立独行的猫a

您的鼓励是我的创作动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值