build protobuf,交叉編譯protobuf-c

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末目锭,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子滥沫,更是在濱河造成了極大的恐慌,老刑警劉巖键俱,帶你破解...
    沈念sama閱讀 221,406評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件兰绣,死亡現(xiàn)場離奇詭異,居然都是意外死亡编振,警方通過查閱死者的電腦和手機缀辩,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,395評論 3 398
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來踪央,“玉大人臀玄,你說我怎么就攤上這事〕澹” “怎么了健无?”我有些...
    開封第一講書人閱讀 167,815評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長液斜。 經(jīng)常有香客問我累贤,道長叠穆,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,537評論 1 296
  • 正文 為了忘掉前任臼膏,我火速辦了婚禮硼被,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘渗磅。我一直安慰自己嚷硫,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 68,536評論 6 397
  • 文/花漫 我一把揭開白布始鱼。 她就那樣靜靜地躺著仔掸,像睡著了一般。 火紅的嫁衣襯著肌膚如雪风响。 梳的紋絲不亂的頭發(fā)上嘉汰,一...
    開封第一講書人閱讀 52,184評論 1 308
  • 那天,我揣著相機與錄音状勤,去河邊找鬼鞋怀。 笑死,一個胖子當(dāng)著我的面吹牛持搜,可吹牛的內(nèi)容都是我干的密似。 我是一名探鬼主播,決...
    沈念sama閱讀 40,776評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼葫盼,長吁一口氣:“原來是場噩夢啊……” “哼残腌!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起贫导,我...
    開封第一講書人閱讀 39,668評論 0 276
  • 序言:老撾萬榮一對情侶失蹤抛猫,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后孩灯,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體闺金,經(jīng)...
    沈念sama閱讀 46,212評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,299評論 3 340
  • 正文 我和宋清朗相戀三年峰档,在試婚紗的時候發(fā)現(xiàn)自己被綠了败匹。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,438評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡讥巡,死狀恐怖掀亩,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情欢顷,我是刑警寧澤槽棍,帶...
    沈念sama閱讀 36,128評論 5 349
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響刹泄,放射性物質(zhì)發(fā)生泄漏外里。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,807評論 3 333
  • 文/蒙蒙 一特石、第九天 我趴在偏房一處隱蔽的房頂上張望盅蝗。 院中可真熱鬧,春花似錦姆蘸、人聲如沸墩莫。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,279評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽狂秦。三九已至,卻和暖如春推捐,著一層夾襖步出監(jiān)牢的瞬間裂问,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,395評論 1 272
  • 我被黑心中介騙來泰國打工牛柒, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留堪簿,地道東北人。 一個月前我還...
    沈念sama閱讀 48,827評論 3 376
  • 正文 我出身青樓皮壁,卻偏偏與公主長得像椭更,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子蛾魄,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,446評論 2 359

推薦閱讀更多精彩內(nèi)容