mipsel-openwrt-linux交叉編譯libwebsockets

mipsel-openwrt-linux交叉編譯libwebsockets


1.下載libwebsockets

官網:https://libwebsockets.org/
GitHub源碼:
git clone https://libwebsockets.org/repo/libwebsockets

2.準備條件

交叉編譯完成zlib(可能不需要)和openssl,目前我還是使用的openssl,雖然它的代碼被很多人詬病,但是目前資料還是較多,暫時仍選用openssl。對於交叉編譯zlib和openssl在其它的總結上有說明,這裏不再細說。編譯安裝玩成後記得其目錄,後面要用。

3.編譯安裝libwebsockets

libwebsockets爲了跨平臺選擇使用cmake生成makefile,所以最好先了解一下cmake,對於編譯過libmysqlclient的人來說,cmake並不陌生。

解壓完libwebsockets源碼後,在其中創建mipsel文件夾,我們將安裝交叉編譯後的程序到該文件夾下。

git clone https://libwebsockets.org/repo/libwebsockets
cd libwebsockets
mkdir mipsel
cd mipsel
vim compile.sh

簡單的編譯腳本:

#########################################################################
# File Name: compile.sh
# Author: loon
# mail: [email protected]
# Created Time: 2018年09月06日 星期四 09時37分51秒
#########################################################################
#!/bin/bash

cd ..
cmake \
    -DCMAKE_INSTALL_PREFIX=/home/ubuntu/work/libwebsockets/mipsel \
    -DCMAKE_C_COMPILER=mipsel-openwrt-linux-gcc-4.8.3 \
    -DCMAKE_CXX_COMPILER=mipsel-openwrt-linux-g++ \
    -DLWS_OPENSSL_INCLUDE_DIRS=/usr/local/ssl-mipsel/include  \
    -DLWS_OPENSSL_LIBRARIES="/usr/local/ssl-mipsel/lib/libssl.so;/usr/local/ssl-mipsel/lib/libcrypto.so"
    #-DZLIB_INCLUDE_DIR=/home/ubuntu/work/zlib-1.2.8/mipsel/include \
    #-DZLIB_LIBRARY=/home/ubuntu/work/zlib-1.2.8/mipsel/lib  \
make
make install

對於cmake的參數需要結合源碼中的CMakeLists.txt和cmake的知識來確認,然後結合自己的交叉編譯器即可。這其中由於庫的不同版本肯定會遇到一些問題,這需要結合問題去解決,後面我將我遇到的一些問題進行了記錄。

4.常見問題

可能出現的錯誤1:

-- Build files have been written to: /home/ubuntu/work/libwebsockets
[  1%] Building C object CMakeFiles/websockets.dir/lib/core/alloc.c.o
In file included from /home/ubuntu/work/libwebsockets/lib/core/private.h:134:0,
                 from /home/ubuntu/work/libwebsockets/lib/core/alloc.c:1:
/home/ubuntu/work/libwebsockets/include/libwebsockets.h:223:25: fatal error: openssl/ssl.h: No such file or directory
 #include <openssl/ssl.h>
                         ^
compilation terminated.
CMakeFiles/websockets.dir/build.make:62: recipe for target 'CMakeFiles/websockets.dir/lib/core/alloc.c.o' failed
make[2]: *** [CMakeFiles/websockets.dir/lib/core/alloc.c.o] Error 1
CMakeFiles/Makefile2:141: recipe for target 'CMakeFiles/websockets.dir/all' failed
make[1]: *** [CMakeFiles/websockets.dir/all] Error 2
Makefile:149: recipe for target 'all' failed
make: *** [all] Error 2
[  1%] Building C object CMakeFiles/websockets.dir/lib/core/alloc.c.o
In file included from /home/ubuntu/work/libwebsockets/lib/core/private.h:134:0,
                 from /home/ubuntu/work/libwebsockets/lib/core/alloc.c:1:
/home/ubuntu/work/libwebsockets/include/libwebsockets.h:223:25: fatal error: openssl/ssl.h: No such file or directory
 #include <openssl/ssl.h>
                         ^
compilation terminated.
CMakeFiles/websockets.dir/build.make:62: recipe for target 'CMakeFiles/websockets.dir/lib/core/alloc.c.o' failed
make[2]: *** [CMakeFiles/websockets.dir/lib/core/alloc.c.o] Error 1
CMakeFiles/Makefile2:141: recipe for target 'CMakeFiles/websockets.dir/all' failed
make[1]: *** [CMakeFiles/websockets.dir/all] Error 2
Makefile:149: recipe for target 'all' failed
make: *** [all] Error 2

編譯時遇到這種問題可以看到是沒有找到相關的openssl的頭文件,可能是沒有正確指明頭文件位置,或者cmake中的指定openssl頭文件的指令有誤。

可能出現的錯誤2:

[ 44%] Linking C executable bin/libwebsockets-test-fuzxy
lib/libwebsockets.a(ssl.c.o): In function `lws_ssl_get_error':
ssl.c:(.text+0x270): undefined reference to `SSL_get_error'
lib/libwebsockets.a(ssl.c.o): In function `lws_ssl_elaborate_error':
ssl.c:(.text+0x444): undefined reference to `ERR_error_string_n'
ssl.c:(.text+0x44c): undefined reference to `ERR_get_error'
lib/libwebsockets.a(ssl.c.o): In function `lws_ssl_bind_passphrase':
ssl.c:(.text+0x488): undefined reference to `SSL_CTX_set_default_passwd_cb_userdata'
ssl.c:(.text+0x4a4): undefined reference to `SSL_CTX_set_default_passwd_cb'
lib/libwebsockets.a(ssl.c.o): In function `lws_context_init_ssl_library':
ssl.c:(.text+0x4d8): undefined reference to `SSL_library_init'
ssl.c:(.text+0x4e0): undefined reference to `OPENSSL_add_all_algorithms_noconf'
ssl.c:(.text+0x4e8): undefined reference to `SSL_load_error_strings'
ssl.c:(.text+0x504): undefined reference to `SSL_get_ex_new_index'
ssl.c:(.text+0x524): undefined reference to `SSL_CTX_get_ex_new_index'
lib/libwebsockets.a(ssl.c.o): In function `lws_ssl_destroy':
ssl.c:(.text+0x574): undefined reference to `SSL_CTX_free'
ssl.c:(.text+0x5a0): undefined reference to `SSL_CTX_free'
ssl.c:(.text+0x5a8): undefined reference to `ERR_remove_thread_state'
ssl.c:(.text+0x5b0): undefined reference to `SSL_COMP_free_compression_methods'
ssl.c:(.text+0x5b8): undefined reference to `ERR_free_strings'
ssl.c:(.text+0x5c0): undefined reference to `EVP_cleanup'
ssl.c:(.text+0x5d0): undefined reference to `CRYPTO_cleanup_all_ex_data'
lib/libwebsockets.a(ssl.c.o): In function `lws_ssl_capable_read':
ssl.c:(.text+0x63c): undefined reference to `SSL_read'
ssl.c:(.text+0x6dc): undefined reference to `SSL_pending'

這種情況明顯是沒有找到鏈接庫,我們結合config生成時的警告來進一步確定:

WARNING: Target "cmTC_2456e" requests linking to directory "/usr/local/ssl-mipsel/lib".  Targets may link only to libraries.  CMake is dropping the item.
-- Looking for SSL_CTX_set1_param - not found
-- Looking for SSL_set_info_callback
WARNING: Target "cmTC_b8022" requests linking to directory "/usr/local/ssl-mipsel/lib".  Targets may link only to libraries.  CMake is dropping the item.
-- Looking for SSL_set_info_callback - not found
-- Looking for X509_VERIFY_PARAM_set1_host
WARNING: Target "cmTC_7f323" requests linking to directory "/usr/local/ssl-mipsel/lib".  Targets may link only to libraries.  CMake is dropping the item.
-- Looking for X509_VERIFY_PARAM_set1_host - not found
-- Looking for RSA_set0_key
WARNING: Target "cmTC_92ac5" requests linking to directory "/usr/local/ssl-mipsel/lib".  Targets may link only to libraries.  CMake is dropping the item.
-- Looking for RSA_set0_key - not found
-- Looking for X509_get_key_usage

可能前面會有類似這樣的警告產生,這是由於庫文件僅僅指明路徑還是不夠的,還需要指明具體的庫文件的名字,比如libssl.so等,需要指明具體的庫的名字(這算是對cmake不熟悉造成的一個比較低級的錯誤)。

應該是:
-DLWS_OPENSSL_LIBRARIES="/usr/local/ssl-mipsel/lib/libssl.so;/usr/local/ssl-mipsel/lib/libcrypto.so"
而不是:
-DLWS_OPENSSL_LIBRARIES=/usr/local/ssl-mipsel/lib

可能出現的錯誤3:

-- Configuring incomplete, errors occurred!
See also "/home/ubuntu/work/libwebsockets/CMakeFiles/CMakeOutput.log".
See also "/home/ubuntu/work/libwebsockets/CMakeFiles/CMakeError.log".
CMake Error: Unable to open check cache file for write. /home/ubuntu/work/libwebsockets/CMakeFiles/cmake.check_cache
Makefile:1937: recipe for target 'cmake_check_build_system' failed
make: *** [cmake_check_build_system] Error 1

目前不是很清楚什麼原因導致了cmake執行時出問題了,目前我的解決方法就是刪除源碼重新下載編譯。

5.最終結果

編譯安裝結果

解決完所有問題後執行我們的腳本就會自動完成配置cmake、編譯和安裝過程。

...
[ 81%] Building C object CMakeFiles/websockets_shared.dir/lib/tls/openssl/openssl-client.c.o
[ 82%] Building C object CMakeFiles/websockets_shared.dir/lib/misc/sha-1.c.o
[ 83%] Building C object CMakeFiles/websockets_shared.dir/lib/plat/unix/unix-caps.c.o
[ 84%] Building C object CMakeFiles/websockets_shared.dir/lib/plat/unix/unix-fds.c.o
[ 85%] Building C object CMakeFiles/websockets_shared.dir/lib/plat/unix/unix-file.c.o
[ 86%] Building C object CMakeFiles/websockets_shared.dir/lib/plat/unix/unix-misc.c.o
[ 87%] Building C object CMakeFiles/websockets_shared.dir/lib/plat/unix/unix-init.c.o
[ 88%] Building C object CMakeFiles/websockets_shared.dir/lib/plat/unix/unix-pipe.c.o
[ 89%] Building C object CMakeFiles/websockets_shared.dir/lib/plat/unix/unix-service.c.o
[ 90%] Building C object CMakeFiles/websockets_shared.dir/lib/plat/unix/unix-sockets.c.o
[ 91%] Building C object CMakeFiles/websockets_shared.dir/lib/roles/http/server/server.c.o
[ 92%] Building C object CMakeFiles/websockets_shared.dir/lib/roles/http/server/lws-spa.c.o
[ 93%] Building C object CMakeFiles/websockets_shared.dir/lib/event-libs/poll/poll.c.o
[ 94%] Building C object CMakeFiles/websockets_shared.dir/lib/misc/lejp.c.o
[ 95%] Building C object CMakeFiles/websockets_shared.dir/lib/roles/http/server/lejp-conf.c.o
[ 96%] Linking C shared library lib/libwebsockets.so
/usr/local/ssl-mipsel/lib/libcrypto.so: warning: gethostbyname is obsolescent, use getnameinfo() instead.
[ 96%] Built target websockets_shared
Scanning dependencies of target test-server-extpoll
[ 97%] Building C object CMakeFiles/test-server-extpoll.dir/test-apps/test-server.c.o
[ 98%] Linking C executable bin/libwebsockets-test-server-extpoll
/usr/local/ssl-mipsel/lib/libcrypto.so: warning: gethostbyname is obsolescent, use getnameinfo() instead.
[ 98%] Built target test-server-extpoll
Scanning dependencies of target test-server
[ 99%] Building C object CMakeFiles/test-server.dir/test-apps/test-server.c.o
[100%] Linking C executable bin/libwebsockets-test-server
/usr/local/ssl-mipsel/lib/libcrypto.so: warning: gethostbyname is obsolescent, use getnameinfo() instead.
[100%] Built target test-server
Install the project...
-- Install configuration: "Release"
-- Installing: /home/ubuntu/work/libwebsockets/mipsel/lib/pkgconfig/libwebsockets.pc
-- Installing: /home/ubuntu/work/libwebsockets/mipsel/lib/pkgconfig/libwebsockets_static.pc
-- Installing: /home/ubuntu/work/libwebsockets/mipsel/include/libwebsockets
-- Installing: /home/ubuntu/work/libwebsockets/mipsel/include/libwebsockets/lws-http.h
-- Installing: /home/ubuntu/work/libwebsockets/mipsel/include/libwebsockets/lws-adopt.h
-- Installing: /home/ubuntu/work/libwebsockets/mipsel/include/libwebsockets/lws-network-helper.h
-- Installing: /home/ubuntu/work/libwebsockets/mipsel/include/libwebsockets/lws-stats.h
-- Installing: /home/ubuntu/work/libwebsockets/mipsel/include/libwebsockets/lws-service.h
-- Installing: /home/ubuntu/work/libwebsockets/mipsel/include/libwebsockets/lws-sha1-base64.h
-- Installing: /home/ubuntu/work/libwebsockets/mipsel/include/libwebsockets/lws-ws-state.h
-- Installing: /home/ubuntu/work/libwebsockets/mipsel/include/libwebsockets/lws-purify.h
-- Installing: /home/ubuntu/work/libwebsockets/mipsel/include/libwebsockets/lws-esp32.h
-- Installing: /home/ubuntu/work/libwebsockets/mipsel/include/libwebsockets/lws-write.h
-- Installing: /home/ubuntu/work/libwebsockets/mipsel/include/libwebsockets/lws-ring.h
-- Installing: /home/ubuntu/work/libwebsockets/mipsel/include/libwebsockets/lws-logs.h
-- Installing: /home/ubuntu/work/libwebsockets/mipsel/include/libwebsockets/lws-vfs.h
-- Installing: /home/ubuntu/work/libwebsockets/mipsel/include/libwebsockets/lws-context-vhost.h
-- Installing: /home/ubuntu/work/libwebsockets/mipsel/include/libwebsockets/lws-jwk.h
-- Installing: /home/ubuntu/work/libwebsockets/mipsel/include/libwebsockets/lws-cgi.h
-- Installing: /home/ubuntu/work/libwebsockets/mipsel/include/libwebsockets/lws-lejp.h
-- Installing: /home/ubuntu/work/libwebsockets/mipsel/include/libwebsockets/lws-jws.h
-- Installing: /home/ubuntu/work/libwebsockets/mipsel/include/libwebsockets/lws-client.h
-- Installing: /home/ubuntu/work/libwebsockets/mipsel/include/libwebsockets/lws-x509.h
-- Installing: /home/ubuntu/work/libwebsockets/mipsel/include/libwebsockets/lws-spa.h
-- Installing: /home/ubuntu/work/libwebsockets/mipsel/include/libwebsockets/lws-ws-ext.h
-- Installing: /home/ubuntu/work/libwebsockets/mipsel/include/libwebsockets/lws-protocols-plugins.h
-- Installing: /home/ubuntu/work/libwebsockets/mipsel/include/libwebsockets/lws-plugin-generic-sessions.h
-- Installing: /home/ubuntu/work/libwebsockets/mipsel/include/libwebsockets/lws-timeout-timer.h
-- Installing: /home/ubuntu/work/libwebsockets/mipsel/include/libwebsockets/lws-misc.h
-- Installing: /home/ubuntu/work/libwebsockets/mipsel/include/libwebsockets/lws-genhash.h
-- Installing: /home/ubuntu/work/libwebsockets/mipsel/include/libwebsockets/lws-writeable.h
-- Installing: /home/ubuntu/work/libwebsockets/mipsel/include/libwebsockets/lws-ws-close.h
-- Installing: /home/ubuntu/work/libwebsockets/mipsel/include/libwebsockets/lws-genrsa.h
-- Installing: /home/ubuntu/work/libwebsockets/mipsel/include/libwebsockets/lws-callbacks.h
-- Installing: /home/ubuntu/work/libwebsockets/mipsel/include/libwebsockets/lws-threadpool.h
-- Installing: /home/ubuntu/work/libwebsockets/mipsel/lib/libwebsockets.a
-- Installing: /home/ubuntu/work/libwebsockets/mipsel/include/libwebsockets.h
-- Installing: /home/ubuntu/work/libwebsockets/mipsel/include/lws_config.h
-- Installing: /home/ubuntu/work/libwebsockets/mipsel/include/lws-plugin-ssh.h
-- Installing: /home/ubuntu/work/libwebsockets/mipsel/lib/libwebsockets.so.13
-- Installing: /home/ubuntu/work/libwebsockets/mipsel/lib/libwebsockets.so
-- Set runtime path of "/home/ubuntu/work/libwebsockets/mipsel/lib/libwebsockets.so.13" to ""
-- Up-to-date: /home/ubuntu/work/libwebsockets/mipsel/include/libwebsockets.h
-- Up-to-date: /home/ubuntu/work/libwebsockets/mipsel/include/lws_config.h
-- Up-to-date: /home/ubuntu/work/libwebsockets/mipsel/include/lws-plugin-ssh.h
-- Installing: /home/ubuntu/work/libwebsockets/mipsel/bin/libwebsockets-test-server
-- Set runtime path of "/home/ubuntu/work/libwebsockets/mipsel/bin/libwebsockets-test-server" to ""
-- Installing: /home/ubuntu/work/libwebsockets/mipsel/bin/libwebsockets-test-fuzxy
-- Set runtime path of "/home/ubuntu/work/libwebsockets/mipsel/bin/libwebsockets-test-fuzxy" to ""
-- Installing: /home/ubuntu/work/libwebsockets/mipsel/bin/libwebsockets-test-server-extpoll
-- Set runtime path of "/home/ubuntu/work/libwebsockets/mipsel/bin/libwebsockets-test-server-extpoll" to ""
-- Installing: /home/ubuntu/work/libwebsockets/mipsel/bin/libwebsockets-test-lejp
-- Set runtime path of "/home/ubuntu/work/libwebsockets/mipsel/bin/libwebsockets-test-lejp" to ""
-- Installing: /home/ubuntu/work/libwebsockets/mipsel/bin/libwebsockets-test-client
-- Set runtime path of "/home/ubuntu/work/libwebsockets/mipsel/bin/libwebsockets-test-client" to ""
-- Installing: /home/ubuntu/work/libwebsockets/mipsel/share/libwebsockets-test-server/libwebsockets-test-server.key.pem
-- Installing: /home/ubuntu/work/libwebsockets/mipsel/share/libwebsockets-test-server/libwebsockets-test-server.pem
-- Installing: /home/ubuntu/work/libwebsockets/mipsel/share/libwebsockets-test-server/favicon.ico
-- Installing: /home/ubuntu/work/libwebsockets/mipsel/share/libwebsockets-test-server/leaf.jpg
-- Installing: /home/ubuntu/work/libwebsockets/mipsel/share/libwebsockets-test-server/candide.zip
-- Installing: /home/ubuntu/work/libwebsockets/mipsel/share/libwebsockets-test-server/libwebsockets.org-logo.png
-- Installing: /home/ubuntu/work/libwebsockets/mipsel/share/libwebsockets-test-server/http2.png
-- Installing: /home/ubuntu/work/libwebsockets/mipsel/share/libwebsockets-test-server/wss-over-h2.png
-- Installing: /home/ubuntu/work/libwebsockets/mipsel/share/libwebsockets-test-server/lws-common.js
-- Installing: /home/ubuntu/work/libwebsockets/mipsel/share/libwebsockets-test-server/test.html
-- Installing: /home/ubuntu/work/libwebsockets/mipsel/share/libwebsockets-test-server/test.css
-- Installing: /home/ubuntu/work/libwebsockets/mipsel/share/libwebsockets-test-server/test.js
-- Installing: /home/ubuntu/work/libwebsockets/mipsel/share/libwebsockets-test-server/private/index.html
-- Installing: /home/ubuntu/work/libwebsockets/mipsel/share/libwebsockets-test-server/lws-ssh-test-keys
-- Installing: /home/ubuntu/work/libwebsockets/mipsel/share/libwebsockets-test-server/lws-ssh-test-keys.pub
-- Installing: /home/ubuntu/work/libwebsockets/mipsel/lib/cmake/libwebsockets/LibwebsocketsConfig.cmake
-- Installing: /home/ubuntu/work/libwebsockets/mipsel/lib/cmake/libwebsockets/LibwebsocketsConfigVersion.cmake
-- Installing: /home/ubuntu/work/libwebsockets/mipsel/lib/cmake/libwebsockets/LibwebsocketsTargets.cmake
-- Installing: /home/ubuntu/work/libwebsockets/mipsel/lib/cmake/libwebsockets/LibwebsocketsTargets-release.cmake

在開發板上的測試結果

這是根據我們配置安裝後的mipsel目錄下的文件目錄樹:

ubuntu@ubuntu-virtual-machine:~/work/libwebsockets/mipsel$ tree -a
.
├── bin
│   ├── libwebsockets-test-client
│   ├── libwebsockets-test-fuzxy
│   ├── libwebsockets-test-lejp
│   ├── libwebsockets-test-server
│   └── libwebsockets-test-server-extpoll
├── compile.sh
├── include
│   ├── libwebsockets
│   │   ├── lws-adopt.h
│   │   ├── lws-callbacks.h
│   │   ├── lws-cgi.h
│   │   ├── lws-client.h
│   │   ├── lws-context-vhost.h
│   │   ├── lws-esp32.h
│   │   ├── lws-genhash.h
│   │   ├── lws-genrsa.h
│   │   ├── lws-http.h
│   │   ├── lws-jwk.h
│   │   ├── lws-jws.h
│   │   ├── lws-lejp.h
│   │   ├── lws-logs.h
│   │   ├── lws-misc.h
│   │   ├── lws-network-helper.h
│   │   ├── lws-plugin-generic-sessions.h
│   │   ├── lws-protocols-plugins.h
│   │   ├── lws-purify.h
│   │   ├── lws-ring.h
│   │   ├── lws-service.h
│   │   ├── lws-sha1-base64.h
│   │   ├── lws-spa.h
│   │   ├── lws-stats.h
│   │   ├── lws-threadpool.h
│   │   ├── lws-timeout-timer.h
│   │   ├── lws-vfs.h
│   │   ├── lws-writeable.h
│   │   ├── lws-write.h
│   │   ├── lws-ws-close.h
│   │   ├── lws-ws-ext.h
│   │   ├── lws-ws-state.h
│   │   └── lws-x509.h
│   ├── libwebsockets.h
│   ├── lws_config.h
│   └── lws-plugin-ssh.h
├── lib
│   ├── cmake
│   │   └── libwebsockets
│   │       ├── LibwebsocketsConfig.cmake
│   │       ├── LibwebsocketsConfigVersion.cmake
│   │       ├── LibwebsocketsTargets.cmake
│   │       └── LibwebsocketsTargets-release.cmake
│   ├── libwebsockets.a
│   ├── libwebsockets.so -> libwebsockets.so.13
│   ├── libwebsockets.so.13
│   └── pkgconfig
│       ├── libwebsockets.pc
│       └── libwebsockets_static.pc
└── share
    └── libwebsockets-test-server
        ├── candide.zip
        ├── favicon.ico
        ├── http2.png
        ├── leaf.jpg
        ├── libwebsockets.org-logo.png
        ├── libwebsockets-test-server.key.pem
        ├── libwebsockets-test-server.pem
        ├── lws-common.js
        ├── lws-ssh-test-keys
        ├── lws-ssh-test-keys.pub
        ├── private
        │   └── index.html
        ├── test.css
        ├── test.html
        ├── test.js
        └── wss-over-h2.png

10 directories, 65 files

將除過編譯文件的內容拷貝到網關中,我們就可以利用bin目錄下的測試服務程序等先測試一下編譯後的libwebsockets是否可以在我們的開發板上運行,測試通過則說明我們也可以利用該庫開發我們的wss程序。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章