1.下载libwebsockets
下载地址:git clone https://github.com/warmcat/libwebsockets.git
2.修改CMakeLists.txt
cd libwebsockets-main
vim CMakeLists.txt
添加下面内容
SET(CMAKE_SYSTEM_NAME Linux)
SET(TOOLCHAIN_DIR "/opt/myir-imx-fb/4.1.15-2.0.1/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-gnueabi")
SET(CMAKE_FIND_ROOT_PATH "${TOOLCHAIN_DIR}")
SET(CMAKE_C_COMPILER "/opt/myir-imx-fb/4.1.15-2.0.1/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-gcc")
SET(CMAKE_CXX_COMPILER "/opt/myir-imx-fb/4.1.15-2.0.1/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-g++")
set(MYSYSROOT /opt/myir-imx-fb/4.1.15-2.0.1/sysroots/cortexa7hf-neon-poky-linux-gnueabi)
# compiler/linker flags
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -march=armv7ve -marm -mfpu=neon -mfloat-abi=hard -mcpu=cortex-a7 --sysroot=${MYSYSROOT}" CACHE INTERNAL "" FORCE)
set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -march=armv7ve -marm -mfpu=neon -mfloat-abi=hard -mcpu=cortex-a7 --sysroot=${MYSYSROOT}" CACHE INTERNAL "" FORCE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --sysroot=${MYSYSROOT}" CACHE INTERNAL "" FORCE)
set(CMAKE_CXX_LINK_FLAGS "${CMAKE_CXX_LINK_FLAGS} --sysroot=${MYSYSROOT}" CACHE INTERNAL "" FORCE)
set(CMAKE_FIND_ROOT_PATH "${MYSYSROOT}")
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
#for libraries and headers in the target directories
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
SET(CMAKE_C_FLAGS"${CMAKE_C_FLAGS} --sysroot=/opt/myir-imx-fb/4.1.15-2.0.1/sysroots/cortexa7hf-neon-poky-linux-gnueabi")
SET(CMAKE_CXX_FLAGS"${CMAKE_CXX_FLAGS} --sysroot=/opt/myir-imx-fb/4.1.15-2.0.1/sysroots/cortexa7hf-neon-poky-linux-gnueabi")
#set(CMAKE_TRY_COMPILE_TARGET_TYPE "STATIC_LIBRARY")
SET(CMAKE_C_COMPILER_WORKS TRUE)
SET(CMAKE_CXX_COMPILER_WORKS TRUE)
注解一:添加这两行作用:直接跳过所有编译器检查, cmake会尝试编译静态库而不是可执行文件,用于交叉编译。
SET(CMAKE_C_COMPILER_WORKS TRUE)
SET(CMAKE_CXX_COMPILER_WORKS TRUE)
或者你可以设置使用这个来代替(直接跳过编译可执行程序)
SET(CMAKE_TRY_COMPILE_TARGET_TYPE "STATIC_LIBRARY")
如果不添加,执行第三步操作:cmake … 报下面错误
-- The CXX compiler identification is GNU 5.3.0
-- Check for working CXX compiler: /opt/myir-imx-fb/4.1.15-2.0.1/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-g++
-- Check for working CXX compiler: /opt/myir-imx-fb/4.1.15-2.0.1/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-g++ -- broken
CMake Error at /usr/share/cmake-3.10/Modules/CMakeTestCXXCompiler.cmake:45 (message):
The C++ compiler
"/opt/myir-imx-fb/4.1.15-2.0.1/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-g++"
is not able to compile a simple test program.
It fails with the following output:
Change Dir: /home/用户名/Documents/libwebsockets-main/build/CMakeFiles/CMakeTmp
Run Build Command:"/usr/bin/make" "cmTC_98ac0/fast"
/usr/bin/make -f CMakeFiles/cmTC_98ac0.dir/build.make CMakeFiles/cmTC_98ac0.dir/build
make[1]: Entering directory '/home/用户名/Documents/libwebsockets-main/build/CMakeFiles/CMakeTmp'
Building CXX object CMakeFiles/cmTC_98ac0.dir/testCXXCompiler.cxx.o
/opt/myir-imx-fb/4.1.15-2.0.1/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-g++ --sysroot=/opt/myir-imx-fb/4.1.15-2.0.1/sysroots/cortexa7hf-neon-poky-linux-gnueabi -o CMakeFiles/cmTC_98ac0.dir/testCXXCompiler.cxx.o -c /home/用户名/Documents/libwebsockets-main/build/CMakeFiles/CMakeTmp/testCXXCompiler.cxx
Linking CXX executable cmTC_98ac0
/usr/bin/cmake -E cmake_link_script CMakeFiles/cmTC_98ac0.dir/link.txt --verbose=1
/opt/myir-imx-fb/4.1.15-2.0.1/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-g++ --sysroot=/opt/myir-imx-fb/4.1.15-2.0.1/sysroots/cortexa7hf-neon-poky-linux-gnueabi -rdynamic CMakeFiles/cmTC_98ac0.dir/testCXXCompiler.cxx.o -o cmTC_98ac0
/opt/myir-imx-fb/4.1.15-2.0.1/sysroots/x86_64-pokysdk-linux/usr/libexec/arm-poky-linux-gnueabi/gcc/arm-poky-linux-gnueabi/5.3.0/real-ld: error: cmTC_98ac0 uses VFP register arguments, CMakeFiles/cmTC_98ac0.dir/testCXXCompiler.cxx.o does not
/opt/myir-imx-fb/4.1.15-2.0.1/sysroots/x86_64-pokysdk-linux/usr/libexec/arm-poky-linux-gnueabi/gcc/arm-poky-linux-gnueabi/5.3.0/real-ld: failed to merge target specific data of file CMakeFiles/cmTC_98ac0.dir/testCXXCompiler.cxx.o
collect2: error: ld returned 1 exit status
CMakeFiles/cmTC_98ac0.dir/build.make:97: recipe for target 'cmTC_98ac0' failed
make[1]: *** [cmTC_98ac0] Error 1
make[1]: Leaving directory '/home/用户名/Documents/libwebsockets-main/build/CMakeFiles/CMakeTmp'
Makefile:126: recipe for target 'cmTC_98ac0/fast' failed
make: *** [cmTC_98ac0/fast] Error 2
CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
minimal-examples-lowlevel/api-tests/api-test-jrpc/CMakeLists.txt:1 (project)
-- Configuring incomplete, errors occurred!
注解二:设置CMAKE_C_FLAGS和CMAKE_CXX_FLAGS 一定要添加 -march=armv7ve -marm -mfpu=neon -mfloat-abi=hard -mcpu=cortex-a7 --sysroot=${MYSYSROOT}如果不添加,出现下面错误
In file included from /opt/myir-imx-fb/4.1.15-2.0.1/sysroots/cortexa7hf-neon-poky-linux-gnueabi/usr/include/features.h:392:0,
from /opt/myir-imx-fb/4.1.15-2.0.1/sysroots/cortexa7hf-neon-poky-linux-gnueabi/usr/include/stdio.h:27,
from /home/用户名/Documents/libwebsockets-main/lib/core/./private-lib-core.h:53,
from /home/用户名/Documents/libwebsockets-main/lib/plat/unix/unix-caps.c:28:
/opt/myir-imx-fb/4.1.15-2.0.1/sysroots/cortexa7hf-neon-poky-linux-gnueabi/usr/include/gnu/stubs.h:7:29: fatal error: gnu/stubs-soft.h: No such file or directory
compilation terminated.
lib/CMakeFiles/websockets_shared.dir/build.make:62: recipe for target 'lib/CMakeFiles/websockets_shared.dir/plat/unix/unix-caps.c.o' failed
make[2]: *** [lib/CMakeFiles/websockets_shared.dir/plat/unix/unix-caps.c.o] Error 1
CMakeFiles/Makefile2:1076: recipe for target 'lib/CMakeFiles/websockets_shared.dir/all' failed
make[1]: *** [lib/CMakeFiles/websockets_shared.dir/all] Error 2
Makefile:162: recipe for target 'all' failed
make: *** [all] Error 2
-mfloat-abi=hard:使用硬浮点,交叉编译器没有软浮点对应的头文件,只有硬浮点对应的头文件
gnu/stubs.h内容:
vim ./4.1.15-2.0.1/sysroots/cortexa7hf-neon-poky-linux-gnueabi/usr/include/gnu/stubs.h
#if !defined __ARM_PCS_VFP
# include <gnu/stubs-soft.h>
#endif
#if defined __ARM_PCS_VFP
# include <gnu/stubs-hard.h>
#endif
可以找到<gnu/stubs-hard.h>
$ find . -name stubs-hard.h
./4.1.15-2.0.1/sysroots/cortexa7hf-neon-poky-linux-gnueabi/usr/include/gnu/stubs-hard.h
3.编译
mkdir build
cd build
cmake .. -DCMAKE_INSTALL_PREFIX=/home/用户名/arm/libwebsockets \
-DCMAKE_SYSTEM_NAME=Linux \
make -j8 && make install
到此编译结束了!!!