- 编译系统:ubuntu20.04
- 编译处理器:x86
- 目标系统:debian
- 目标处理器:arm-v7
- 编译工具:arm-linux-gnueabihf
交叉编译openssl
-
下载openssl-1.0.1u(注意版本号,不同会出错)
可以在官网下载,觉得麻烦可以支持一下
https://download.csdn.net/download/weixin_41736398/19081601 -
解压openssl-1.0.1u.tar.gz
tar -vxzf openssl-1.0.1u.tar.gz -
交叉编译openssl
- ./config -no-asm --prefix=target_file 生成Makefile(–prefix=target_file为编译成功后,安装的位置,可不设置,默认在/usr中)
- 修改 Makefile 中的CC
#CC=gcc
CC=/opt/gcc-arm-linux-gnueabihf-8.3.0/bin/arm-linux-gnueabihf-gcc
- 去除编译和连接选项 -m64
- 执行make&make install 编译和安装openssl库(在target_file中生成库文件)
交叉编译websocket
-
下载libwebsocket-v3.2.2(注意版本号,不同会出错)
可以在官网下载,觉得麻烦可以支持一下
https://download.csdn.net/download/weixin_41736398/19081601 -
交叉编译
-
cd libwebsockets-v3.2.2/
-
touch cross-arm-linux-gnueabihf.cmake
-
vim cross-arm-linux-gnueabihf.cmake 添加以下内容
# # CMake Toolchain file for crosscompiling on ARM. # # This can be used when running cmake in the following way: # cd build/ # cmake .. -DCMAKE_TOOLCHAIN_FILE=../cross-arm-linux-gnueabihf.cmake # # Target operating system name. set(CMAKE_SYSTEM_NAME Linux) # Name of C compiler. #openssl交叉编译生成的文件夹路径,可根据需求更改 set(CMAKE_FIND_ROOT_PATH "/home/ubuntu/0.documen/0.material/openssl/install/openssl-1.0.1/") #交叉编译工具路径,可根据需要更改,建议8.3.0版本 set(CROSS_PATH /opt/gcc-arm-linux-gnueabihf-8.3.0) set(CMAKE_C_COMPILER "${CROSS_PATH}/bin/arm-linux-gnueabihf-gcc") set(CMAKE_CXX_COMPILER "${CROSS_PATH}/bin/arm-linux-gnueabihf-g++") # # Different build system distros set release optimization level to different # things according to their local policy, eg, Fedora is -O2 and Ubuntu is -O3 # here. Actually the build system's local policy is completely unrelated to # our desire for cross-build release optimization policy for code built to run # on a completely different target than the build system itself. # # Since this goes last on the compiler commandline we have to override it to a # sane value for cross-build here. Notice some gcc versions enable broken # optimizations with -O3. # if (CMAKE_BUILD_TYPE MATCHES RELEASE OR CMAKE_BUILD_TYPE MATCHES Release OR CMAKE_BUILD_TYPE MATCHES release) set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O2") set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O2") endif() set(CMAKE_INSTALL_PREFIX "/home/ubuntu/0.documen/0.material/websocket/install/websocket-v3.2.2") # Where to look for the target environment. (More paths can be added here) set(OPENSSL_EXECUTABLE 0) # Adjust the default behavior of the FIND_XXX() commands: # search programs in the host environment only. set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) # Search headers and libraries in the target environment only. set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
-
mkdir build
-
cd build
-
openssl genrsa -out libwebsockets-test-server.key.pem 2048
-
openssl req -new -x509 -key libwebsockets-test-server.key.pem -out libwebsockets-test-server.pem -days 1095(一路回车)
-
make &make install
-
-
项目交叉编译
-
将openssl-1.0.1u,libwebsocket-v3.2.2生成的文件复制到项目中lib文件夹中
-
CMakeLists.txt添加以下内容
set(CMAKE_CXX_STANDARD 14) set(CROSS_PATH /opt/gcc-arm-linux-gnueabihf-8.3.0) # Name of C compiler. set(CMAKE_C_COMPILER "${CROSS_PATH}/bin/arm-linux-gnueabihf-gcc") set(CMAKE_CXX_COMPILER "${CROSS_PATH}/bin/arm-linux-gnueabihf-g++") set(CMAKE_FIND_ROOT_PATH "./lib") set(OPENSSL_EXECUTABLE 0) # Adjust the default behavior of the FIND_XXX() commands: # search programs in the host environment only. set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) # Search headers and libraries in the target environment only. set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) link_directories("lib/openssl-1.0.1/lib") link_directories("lib/websocket3.2.2/lib") include_directories("lib/openssl-1.0.1/include") include_directories("lib/websocket3.2.2/include") add_executable(ws_cross_compile src/main.cpp) target_link_libraries(ws_cross_compile -lwebsockets) target_link_libraries(ws_cross_compile -lcrypto) target_link_libraries(ws_cross_compile -lssl) find_package(Threads REQUIRED) target_link_libraries(ws_cross_compile Threads::Threads)
-
编译后复制到arm板后,运行,会出现以下错误
./cross_compile_gateway: error while loading shared libraries: libwebsockets.so.15: cannot open shared object file: No such file or directory
解决办法:
修改/etc/ld.so.conf文件
添加缺少动态库连接对应路径,如
- sudo vim /etc/ld.so.conf
include /etc/ld.so.conf.d/*.conf /home/debian/nfs_share/code/cross_compile_gateway/lib/openssl-1.0.1/lib /home/debian/nfs_share/code/cross_compile_gateway/lib/websocket3.2.2/lib
- sudo ldconfig
-