admin 管理员组

文章数量: 887042

mipsel

mipsel-openwrt-linux交叉编译libwebsockets


  • mipsel-openwrt-linux交叉编译libwebsockets
    • 1.下载libwebsockets
    • 2.准备条件
    • 3.编译安装libwebsockets
    • 4.常见问题
      • 可能出现的错误1:
      • 可能出现的错误2:
      • 可能出现的错误3:
    • 5.最终结果
      • 编译安装结果
      • 在开发板上的测试结果

1.下载libwebsockets

官网:/
GitHub源码:
git clone

2.准备条件

交叉编译完成zlib(可能不需要)和openssl,目前我还是使用的openssl,虽然它的代码被很多人诟病,但是目前资料还是较多,暂时仍选用openssl。对于交叉编译zlib和openssl在其它的总结上有说明,这里不再细说。编译安装玩成后记得其目录,后面要用。

3.编译安装libwebsockets

libwebsockets为了跨平台选择使用cmake生成makefile,所以最好先了解一下cmake,对于编译过libmysqlclient的人来说,cmake并不陌生。

解压完libwebsockets源码后,在其中创建mipsel文件夹,我们将安装交叉编译后的程序到该文件夹下。

git clone 
cd libwebsockets
mkdir mipsel
cd mipsel
vim compile.sh

简单的编译脚本:

#########################################################################
# File Name: compile.sh
# Author: loon
# mail: 2453419889@qq
# Created Time: 2018年09月06日 星期四 09时37分51秒
#########################################################################
#!/bin/bashcd ..
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-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-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.png10 directories, 65 files

将除过编译文件的内容拷贝到网关中,我们就可以利用bin目录下的测试服务程序等先测试一下编译后的libwebsockets是否可以在我们的开发板上运行,测试通过则说明我们也可以利用该库开发我们的wss程序。

本文标签: mipsel