1,編譯protobuf
可以在protobuf git上面找到PC上扛门,比如ubuntu下編譯protobuf的方法,因為ARM下使用的protobuf-c依賴于protobuf庫心傀,所以你必須先編譯protobuf for PC,比如我的環(huán)境是ubuntu 16.0孔祸,找到編譯方法,如下圖所示发皿。
比如我需要在C++環(huán)境下編譯崔慧,則選擇src,鏈接如下:C++ 編譯鏈接
You can also get the source by "git clone" our git repository. Make sure you have also cloned the submodules and generated the configure script (skip this if you are using a release .tar.gz or .zip package):
$ git clone https://github.com/protocolbuffers/protobuf.git
$ cd protobuf
$ git submodule update --init --recursive
$ ./autogen.sh
To build and install the C++ Protocol Buffer runtime and the Protocol Buffer compiler (protoc) execute the following:
$ ./configure
$ make
$ make check
$ sudo make install
$ sudo ldconfig # refresh shared library cache.
2穴墅,編譯protobuf-c for PC
1)下載[Protobuf-C源碼](https://github.com/protobuf-c/protobuf-c.git)
2)編譯PC版本的Protobuf-c文件
? ?(1)執(zhí)行./autogen.sh
? ? (2)執(zhí)行./configure
? ? (3)? ? CC=<路徑>/bin/aarch64-openwrt-linux-gcc
?????????????CXX=<路徑>/aarch64-openwrt-linux-g++ ./configure --host=aarch64-openwrt-linux
?(4)執(zhí)行make && sudo make install 就可以生成protoc-c惶室、頭文件、庫文件(路徑/usr/local/)玄货,其中protoc-c用于將.pro文件生成.c .h文件皇钞,供應(yīng)用使用。
(5)pc上的例子[example.tar.gz](/_attachment/2018-08-10/example.tar.gz
或者通過如下命令一步完成操作:sudo ./autogen.sh && sudo ./configure && sudo make && sudo make install
成功編譯后輸出:
----------------------------------------------------------------------
Libraries have been installed in:
? /usr/local/lib
If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the '-LLIBDIR'
flag during linking and do at least one of the following:
? - add LIBDIR to the 'LD_LIBRARY_PATH' environment variable
? ? during execution
? - add LIBDIR to the 'LD_RUN_PATH' environment variable
? ? during linking
? - use the '-Wl,-rpath -Wl,LIBDIR' linker flag
? - have your system administrator add LIBDIR to '/etc/ld.so.conf'
See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
編譯過程中遇到了一些問題松捉,這些問題在源碼github上的issue中有解決
https://github.com/protobuf-c/protobuf-c/pull/342/files
3夹界,交叉編譯protobuf-c for ARM
使用同一份protobuf-c代碼,因為生成代碼和編譯在PC上編譯隘世,板子上只是運行應(yīng)用程序可柿。
1)make clean
2) ./configure --host=arm64-linux CC=<PATH_YOUR_COMPILER>/arm64-linux-gcc CXX=<PATH_YOUR_COMPILER>/arm64-linux-g++ --disable-protoc --prefix=`pwd`/build/protobuf-c-arm
(像我的路徑就如下:
./configure --host=aarch64-openwrt-linux CC=/home/xx/share/compile_tools/toolchain-aarch64_armv8-a_gcc-4.8-linaro_glibc-2.19/bin/aarch64-openwrt-linux-gcc CXX=/home/xx/share/compile_tools/toolchain-aarch64_armv8-a_gcc-4.8-linaro_glibc-2.19/bin/aarch64-openwrt-linux-g++ --disable-protoc --prefix=`pwd`/build/protobuf-c-arm )
3) sudo make && sudo make install
得到編譯成功的反饋:
----------------------------------------------------------------------
Libraries have been installed in:
? /home/xx/share/openSource/protobuf-c-master/build/protobuf-c-arm/lib
If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the '-LLIBDIR'
flag during linking and do at least one of the following:
? - add LIBDIR to the 'LD_LIBRARY_PATH' environment variable
? ? during execution
? - add LIBDIR to the 'LD_RUN_PATH' environment variable
? ? during linking
? - use the '-Wl,-rpath -Wl,LIBDIR' linker flag
? - have your system administrator add LIBDIR to '/etc/ld.so.conf'
See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
使用readelf -h libprotobuf.so來查看文件信息,如果對應(yīng)machin值是你的目標主機丙者,如arm或者aarch64,那么就是大功告成了复斥。
readelf -h build/protobuf-c-arm/lib/libprotobuf-c.so
筆者的查看結(jié)果如下:
ELF Header:
? Magic:? 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
? Class:? ? ? ? ? ? ? ? ? ? ? ? ? ? ELF64
? Data:? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 2's complement, little endian
? Version:? ? ? ? ? ? ? ? ? ? ? ? ? 1 (current)
? OS/ABI:? ? ? ? ? ? ? ? ? ? ? ? ? ? UNIX - System V
? ABI Version:? ? ? ? ? ? ? ? ? ? ? 0
? Type:? ? ? ? ? ? ? ? ? ? ? ? ? ? ? DYN (Shared object file)
? Machine:? ? ? ? ? ? ? ? ? ? ? ? ? AArch64
? Version:? ? ? ? ? ? ? ? ? ? ? ? ? 0x1
? Entry point address:? ? ? ? ? ? ? 0xd40
? Start of program headers:? ? ? ? ? 64 (bytes into file)
? Start of section headers:? ? ? ? ? 165264 (bytes into file)
? Flags:? ? ? ? ? ? ? ? ? ? ? ? ? ? 0x0
? Size of this header:? ? ? ? ? ? ? 64 (bytes)
? Size of program headers:? ? ? ? ? 56 (bytes)
? Number of program headers:? ? ? ? 3
? Size of section headers:? ? ? ? ? 64 (bytes)
? Number of section headers:? ? ? ? 34
? Section header string table index: 31
使用protobuf-c:
Use the?protoc?command to generate?.pb-c.c?and?.pb-c.h?output files from your?.proto?input file. The?--c_out?options instructs?protoc?to use the protobuf-c plugin.
protoc-c --c_out=. example.proto
Include the?.pb-c.h?file from your C source code.
#include "example.pb-c.h"
使用中如果發(fā)現(xiàn)錯誤,protoc: error while loading shared libraries: libprotoc.so.17: cannot open shared object file: No such file or directory
是protoc的$LD_LIBRARY_PATH沒有設(shè)置械媒,
export LD_LIBRARY_PATH=/usr/local/lib
protoc --c_out=. example.proto
export PATH=$PATH:/usr/local/lib
然后執(zhí)行source ~/.bashrc