交叉編譯libwebsockets

Complex Cross compiling example

Here are step by step instructions for cross-building the external projects needed for lws with lwsws + mbedtls as an example.

In the example, my toolchain lives in <tool_chain_file>/compile_tools/toolchain-aarch64_armv8-a_gcc-4.8-linaro_glibc-2.19 and is named arm-openwrt-linux-muslgnueabi. So you will need to adapt those to where your toolchain lives and its name where you see them here.

Likewise I do all this in /tmp but it has no special meaning, you can adapt that to somewhere else.

All "foreign" cross-built binaries are sent into /tmp/cross so they cannot be confused for 'native' x86_64 stuff on your host machine in /usr/[local/]....

Prepare the cmake toolchain file

1) cd /tmp

2) wget -O mytoolchainfile https://raw.githubusercontent.com/warmcat/libwebsockets/master/contrib/cross-arm-linux-gnueabihf.cmake

3) Edit /tmp/mytoolchainfile adapting CROSS_PATH, CMAKE_C_COMPILER and CMAKE_CXX_COMPILER to reflect your toolchain install dir and path to your toolchain C and C++ compilers respectively. For my case:

set(CROSS_PATH <tool_chain_file>/compile_tools/toolchain-aarch64_armv8-a_gcc-4.8-linaro_glibc-2.19/)

set(CMAKE_C_COMPILER "${CROSS_PATH}/bin/arm-openwrt-linux-muslgnueabi-gcc")

set(CMAKE_CXX_COMPILER "${CROSS_PATH}/bin/arm-openwrt-linux-muslgnueabi-g++")

libsockets:::

cmake .. -DCMAKE_INSTALL_PREFIX:PATH=<tool_chain_file>/compile_tools/toolchain-aarch64_armv8-a_gcc-4.8-linaro_glibc-2.19 \

? ? -DCMAKE_TOOLCHAIN_FILE=../cross-arm-linux-gnueabihf.cmake \

? ? -DLWS_WITHOUT_EXTENSIONS=1 -DLWS_WITH_SSL=0 \

? ? -DLWS_WITH_ZIP_FOPS=0 -DLWS_WITH_ZLIB=0

lib/libmbedtls:::

cmake .. -DCMAKE_TOOLCHAIN_FILE=../cross-arm-linux-gnueabihf.cmake -DCMAKE_INSTALL_PREFIX:PATH=<tool_chain_file>/compile_tools/toolchain-aarch64_armv8-a_gcc-4.8-linaro_glibc-2.19 -DCMAKE_BUILD_TYPE=RELEASE -DUSE_SHARED_MBEDTLS_LIBRARY=1

libsockets:::

cmake .. -DCMAKE_TOOLCHAIN_FILE=<tool_chain_file>/compile_tools/toolchain-aarch64_armv8-a_gcc-4.8-linaro_glibc-2.19 \

-DCMAKE_INSTALL_PREFIX:PATH=<tool_chain_file>/compile_tools/toolchain-aarch64_armv8-a_gcc-4.8-linaro_glibc-2.19 \

-DLWS_WITH_LWSWS=1 \

-DLWS_WITH_MBEDTLS=1 \

-DLWS_MBEDTLS_LIBRARIES="../../mbedtls-2.11.0/build/library/libmbedtls.so" \

-DLWS_MBEDTLS_INCLUDE_DIRS=../../mbedtls-2.11.0/include/mbedtls \

1/4: Building libuv cross:

1) export PATH=<tool_chain_file>/compile_tools/toolchain-aarch64_armv8-a_gcc-4.8-linaro_glibc-2.19/bin:$PATH

2) cd /tmp?

###mkdir cross we will put the cross-built libs in /tmp/cross

3) git clone https://github.com/libuv/libuv.git ##get libuv

4) cd libuv

5) ./autogen.sh

6) ./configure --host=aarch64-openwrt-linux-gnu --prefix=/tmp/cross

7) make && make install this will install to /tmp/cross/...

8) file /tmp/cross/lib/libuv.so.1.0.0 Check it's really built for ARM

/tmp/cross/lib/libuv.so.1.0.0: ELF 64-bit LSB shared object, ARM aarch64, version 1 (SYSV), dynamically linked, not stripped

2/4: Building zlib cross

1) cd /tmp

2) git clone https://github.com/madler/zlib.git

3) cd /tmp/zlib

4) CC=aarch64-openwrt-linux-gnu-gcc ./configure --prefix=/tmp/cross

5) make && make install

6) file /tmp/cross/lib/libz.so.1.2.11 This is just to confirm we built an ARM lib as expected

/tmp/cross/lib/libz.so.1.2.11: ELF 64-bit LSB shared object, ARM aarch64, version 1 (SYSV), dynamically linked, not stripped

3/4: Building mbedtls cross

1) cd /tmp

2) git clone https://github.com/ARMmbed/mbedtls.git

3) cd mbedtls ; mkdir build ; cd build

3) cmake .. -DCMAKE_TOOLCHAIN_FILE=../cross-arm-linux-gnueabihf.cmake -DCMAKE_INSTALL_PREFIX:PATH=/tmp/cross -DCMAKE_BUILD_TYPE=RELEASE -DUSE_SHARED_MBEDTLS_LIBRARY=1

## mbedtls also uses cmake, so you can simply reuse the toolchain file you used for libwebsockets. That is why you shouldn't put project-specific options in the toolchain file, it should just describe the toolchain.

4) make && make install

5) file /tmp/cross/lib/libmbedcrypto.so.2.6.0

/tmp/cross/lib/libmbedcrypto.so.2.13.1: ELF 64-bit LSB shared object, ARM aarch64, version 1 (SYSV), dynamically linked, not stripped

4/4: Building libwebsockets with everything

1) cd /tmp

2) git clone ssh://git@github.com/warmcat/libwebsockets

3) cd libwebsockets ; mkdir build ; cd build

4) (this is all one line on the commandline)

cmake .. -DCMAKE_TOOLCHAIN_FILE=../cross-arm-linux-gnueabihf.cmake \

-DCMAKE_INSTALL_PREFIX:PATH=/tmp/cross \

-DLWS_WITH_LWSWS=1 \

-DLWS_WITH_MBEDTLS=1 \

-DLWS_MBEDTLS_LIBRARIES="/tmp/cross/lib/libmbedcrypto.so;/tmp/cross/lib/libmbedtls.so;/tmp/cross/lib/libmbedx509.so" \

-DLWS_MBEDTLS_INCLUDE_DIRS=/tmp/cross/include \

-DLWS_LIBUV_LIBRARIES=/tmp/cross/lib/libuv.so \

-DLWS_LIBUV_INCLUDE_DIRS=/tmp/cross/include \

-DLWS_ZLIB_LIBRARIES=/tmp/cross/lib/libz.so \

-DLWS_ZLIB_INCLUDE_DIRS=/tmp/cross/include

3) make && make install

4) file /tmp/cross/lib/libwebsockets.so

/tmp/cross/lib/libwebsockets.so: ELF 64-bit LSB shared object, ARM aarch64, version 1 (SYSV), dynamically linked, not stripped

5) aarch64-openwrt-linux-objdump -p /tmp/cross/lib/libwebsockets.so | grep NEEDED

## Confirm that the lws library was linked against everything we expect (libm / libc are provided by your toolchain)

? NEEDED? ? ? ? ? ? ? libmbedtls.so

? NEEDED? ? ? ? ? ? ? libuv.so.1

? NEEDED? ? ? ? ? ? ? libm.so.6

? NEEDED? ? ? ? ? ? ? libgcc_s.so.1

? NEEDED? ? ? ? ? ? ? libc.so.6

You will also find the lws test apps in /tmp/cross/bin... to run lws on the target you will need to copy the related things from /tmp/cross... all the .so from /tmp/cross/lib and anything from /tmp/cross/bin you want.

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子窒所,更是在濱河造成了極大的恐慌隘擎,老刑警劉巖啊鸭,帶你破解...
    沈念sama閱讀 218,204評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件奥溺,死亡現(xiàn)場離奇詭異摔吏,居然都是意外死亡媒怯,警方通過查閱死者的電腦和手機订讼,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,091評論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來扇苞,“玉大人欺殿,你說我怎么就攤上這事寄纵。” “怎么了脖苏?”我有些...
    開封第一講書人閱讀 164,548評論 0 354
  • 文/不壞的土叔 我叫張陵程拭,是天一觀的道長。 經(jīng)常有香客問我棍潘,道長恃鞋,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,657評論 1 293
  • 正文 為了忘掉前任蜒谤,我火速辦了婚禮山宾,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘鳍徽。我一直安慰自己资锰,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,689評論 6 392
  • 文/花漫 我一把揭開白布阶祭。 她就那樣靜靜地躺著绷杜,像睡著了一般。 火紅的嫁衣襯著肌膚如雪濒募。 梳的紋絲不亂的頭發(fā)上鞭盟,一...
    開封第一講書人閱讀 51,554評論 1 305
  • 那天,我揣著相機與錄音瑰剃,去河邊找鬼齿诉。 笑死,一個胖子當(dāng)著我的面吹牛晌姚,可吹牛的內(nèi)容都是我干的粤剧。 我是一名探鬼主播栓票,決...
    沈念sama閱讀 40,302評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼养篓,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了挠日?” 一聲冷哼從身側(cè)響起宝磨,我...
    開封第一講書人閱讀 39,216評論 0 276
  • 序言:老撾萬榮一對情侶失蹤弧关,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后唤锉,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體世囊,經(jīng)...
    沈念sama閱讀 45,661評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,851評論 3 336
  • 正文 我和宋清朗相戀三年窿祥,在試婚紗的時候發(fā)現(xiàn)自己被綠了茸习。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,977評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡壁肋,死狀恐怖号胚,靈堂內(nèi)的尸體忽然破棺而出籽慢,到底是詐尸還是另有隱情,我是刑警寧澤猫胁,帶...
    沈念sama閱讀 35,697評論 5 347
  • 正文 年R本政府宣布箱亿,位于F島的核電站,受9級特大地震影響弃秆,放射性物質(zhì)發(fā)生泄漏届惋。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,306評論 3 330
  • 文/蒙蒙 一菠赚、第九天 我趴在偏房一處隱蔽的房頂上張望脑豹。 院中可真熱鬧,春花似錦衡查、人聲如沸瘩欺。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,898評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽俱饿。三九已至,卻和暖如春塌忽,著一層夾襖步出監(jiān)牢的瞬間拍埠,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,019評論 1 270
  • 我被黑心中介騙來泰國打工土居, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留枣购,地道東北人。 一個月前我還...
    沈念sama閱讀 48,138評論 3 370
  • 正文 我出身青樓擦耀,卻偏偏與公主長得像棉圈,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子埂奈,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,927評論 2 355

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