文章目录
- 环境
- 编译流程
- nasm编译
- x264编译
- zlib编译
- libJPEG编译
- libPNG编译
- libtiff编译
- FFmpeg编译
- OpenCV编译
- 问题1
- 解决方案
- 问题2
- 解决方案
- 总结
环境
- 系统:Ubutu 18.04
- 交叉编译链:gcc-arm-10.2-2020.11-x86_64-aarch64-none-linux-gnu 我的路径/opt/toolchain/gcc-arm-10.2-2020.11-x86_64-aarch64-none-linux-gnu/bin/aarch64-none-linux-gnu-gcc
- OpenCV 3.4.16
- FFmpeg n3.4.12
- zlib 1.3.1
- 好多包找不到哪里下的了备份在这里
编译流程
nasm编译
- 下载源码并解压
mkdir build && cd build
../configure --host=aarch64-linux --prefix=/home/lei/RK_Lib/nasm-lib --enable-shared CC=/opt/toolchain/gcc-arm-10.2-2020.11-x86_64-aarch64-none-linux-gnu/bin/aarch64-none-linux-gnu-gcc --disable-asm
x264编译
- 下载源码并解压
mkdir build && cd build
../configure --enable-shared --enable-static --disable-asm --prefix=/home/lei/RK_Lib/x264-lib --host=aarch64-linux --cross-prefix=/opt/toolchain/gcc-arm-10.2-2020.11-x86_64-aarch64-none-linux-gnu/bin/aarch64-none-linux-gnu- --extra-libs=-ldl --disable-opencl
zlib编译
- 下载源码并解压
mkdir build && cd build
../configure --prefix=/home/lei/RK_Lib/zlib-lib
修改makefile: 替换gcc为/opt/toolchain/gcc-arm-10.2-2020.11-x86_64-aarch64-none-linux-gnu/bin/aarch64-none-linux-gnu-gcc
libJPEG编译
- 下载源码并解压
mkdir build && cd build
../configure CC=/opt/toolchain/gcc-arm-10.2-2020.11-x86_64-aarch64-none-linux-gnu/bin/aarch64-none-linux-gnu-gcc --host=aarch64-linux --prefix=/home/lei/RK_Lib/JPEG-lib --enable-shared --enable-static
libPNG编译
- 下载源码并解压
mkdir build && cd build
- 将zlib 下的inlcue中的文件和lib中的文件拷贝交叉编译器下的目录/opt/toolchain/gcc-arm-10.2-2020.11-x86_64-aarch64-none-linux-gnu/gcc-arm-10.2-2020.11-x86_64-aarch64-none-linux-gnu/include lib
../configure CC=/opt/toolchain/gcc-arm-10.2-2020.11-x86_64-aarch64-none-linux-gnu/bin/aarch64-none-linux-gnu-gcc --host=aarch64-linux --prefix=/home/lei/RK_Lib/PNG-lib --enable-shared --enable-static
libtiff编译
- 下载源码并解压
mkdir build && cd build
../configure CC=/opt/toolchain/gcc-arm-10.2-2020.11-x86_64-aarch64-none-linux-gnu/bin/aarch64-none-linux-gnu-gcc CXX=/opt/toolchain/gcc-arm-10.2-2020.11-x86_64-aarch64-none-linux-gnu/bin/aarch64-none-linux-gnu-g++ --host=arm-linux --prefix=/home/lei/RK_Lib/tiff-lib --enable-shared --enable-static
FFmpeg编译
- 下载源码并解压
mkdir build && cd build
- 创建/home/lei/RK_Lib/arm-lib/include 和 /home/lei/RK_Lib/arm-lib/lib并将x264和zlib编译出的include和lib复制进去
../configure --enable-shared --enable-static --prefix=/home/lei/RK_Lib/ffmpeg-lib3 --cross-prefix=/opt/toolchain/gcc-arm-10.2-2020.11-x86_64-aarch64-none-linux-gnu/bin/aarch64-none-linux-gnu- --arch=aarch64 --target-os=linux --enable-gpl --enable-ffmpeg --enable-libx264 --enable-zlib --enable-avresample --extra-cflags=-I/home/lei/RK_Lib/arm-lib/include --extra-ldflags=-L/home/lei/RK_Lib/arm-lib/lib
OpenCV编译
- 下载源码并解压
mkdir build && cd build
- 将编译出的FFmpeg的include和lib也复制到/home/lei/RK_Lib/arm-lib/include 和 /home/lei/RK_Lib/arm-lib/lib
- /home/lei/RK_Lib/arm-lib目录下创建ffmpeg-config.cmake并编辑
set(ffmpeg_path "${CMAKE_CURRENT_LIST_DIR}")
message("ffmpeg_path: ${ffmpeg_path}")
set(FFMPEG_EXEC_DIR "${ffmpeg_path}/bin")
set(FFMPEG_LIBDIR "${ffmpeg_path}/lib")
set(FFMPEG_INCLUDE_DIRS "${ffmpeg_path}/include")
# library names
set(FFMPEG_LIBRARIES
${FFMPEG_LIBDIR}/libavformat.so
${FFMPEG_LIBDIR}/libavdevice.so
${FFMPEG_LIBDIR}/libavcodec.so
${FFMPEG_LIBDIR}/libavutil.so
${FFMPEG_LIBDIR}/libswscale.so
${FFMPEG_LIBDIR}/libswresample.so
${FFMPEG_LIBDIR}/libavfilter.so
${FFMPEG_LIBDIR}/libavresample.so
${FFMPEG_LIBDIR}/libz.so
${FFMPEG_LIBDIR}/libx264.so
)
# found status
set(FFMPEG_libavformat_FOUND TRUE)
set(FFMPEG_libavdevice_FOUND TRUE)
set(FFMPEG_libavcodec_FOUND TRUE)
set(FFMPEG_libavutil_FOUND TRUE)
set(FFMPEG_libswscale_FOUND TRUE)
set(FFMPEG_libswresample_FOUND TRUE)
set(FFMPEG_libavfilter_FOUND TRUE)
set(FFMPEG_libavresample_FOUND TRUE)
# library versions, 注意这几个变量,一定要设置为全局CACHE变量
set(FFMPEG_libavutil_VERSION 55.78.100 CACHE INTERNAL "FFMPEG_libavutil_VERSION") # info
set(FFMPEG_libavcodec_VERSION 57.107.100 CACHE INTERNAL "FFMPEG_libavcodec_VERSION") # info
set(FFMPEG_libavformat_VERSION 57.83.100 CACHE INTERNAL "FFMPEG_libavformat_VERSION") # info
set(FFMPEG_libavdevice_VERSION 57.10.100 CACHE INTERNAL "FFMPEG_libavdevice_VERSION") # info
set(FFMPEG_libavfilter_VERSION 6.107.100 CACHE INTERNAL "FFMPEG_libavfilter_VERSION") # info
set(FFMPEG_libswscale_VERSION 4.8.100 CACHE INTERNAL "FFMPEG_libswscale_VERSION") # info
set(FFMPEG_libswresample_VERSION 2.9.100 CACHE INTERNAL "FFMPEG_libswresample_VERSION") # info
set(FFMPEG_libavresample_VERSION 3.7.0 CACHE INTERNAL "FFMPEG_libavresample_VERSION") # info
set(FFMPEG_FOUND TRUE)
set(FFMPEG_LIBS ${FFMPEG_LIBRARIES})
status(" #################################### FFMPEG:" FFMPEG_FOUND THEN "YES (find_package)" ELSE "NO (find_package)")
status(" avcodec:" FFMPEG_libavcodec_VERSION THEN "YES (${FFMPEG_libavcodec_VERSION})" ELSE NO)
status(" avformat:" FFMPEG_libavformat_VERSION THEN "YES (${FFMPEG_libavformat_VERSION})" ELSE NO)
status(" avutil:" FFMPEG_libavutil_VERSION THEN "YES (${FFMPEG_libavutil_VERSION})" ELSE NO)
status(" swscale:" FFMPEG_libswscale_VERSION THEN "YES (${FFMPEG_libswscale_VERSION})" ELSE NO)
status(" avresample:" FFMPEG_libavresample_VERSION THEN "YES (${FFMPEG_libavresample_VERSION})" ELSE NO)
注意这里需要把zlib和x264的库文件也指定好
-进入opencv/build目录下,执行 vim toolchain.cmake
并写入以下内容,具体路径自行指定:
set( CMAKE_SYSTEM_NAME Linux )
set( CMAKE_SYSTEM_PROCESSOR aarch64 )
set( CMAKE_C_COMPILER /opt/toolchain/gcc-arm-10.2-2020.11-x86_64-aarch64-none-linux-gnu/bin/aarch64-none-linux-gnu-gcc )
set( CMAKE_CXX_COMPILER /opt/toolchain/gcc-arm-10.2-2020.11-x86_64-aarch64-none-linux-gnu/bin/aarch64-none-linux-gnu-g++ )
set( CMAKE_FIND_ROOT_PATH "/home/lei/RK_Lib/arm-lib/lib" )
set( CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER )
set( CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY )
set( CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY )
- 修改OpenCV的CMakeLists.txt
# opencv.hpp and legacy headers
link_directories(/home/lei/RK_Lib/arm-lib/lib) # 添加这句
add_subdirectory(include)
-
修改OpenCVFindLibsVideo.cmake把
message(FATAL_ERROR "FFMPEG: test check build log:\n${TRY_OUT}")
取消注释,以便查看FFmpeg编译输出信息 -
终端执行
cmake -D CMAKE_TOOLCHAIN_FILE=toolchain.cmake \
-D BUILD_opencv_python3=yes -D BUILD_opencv_python2=no -D PYTHON3_EXECUTABLE=/usr/bin/python3.6m -D PYTHON3_INCLUDE_DIR=/usr/include/python3.6m -D PYTHON3_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.6m.so -D PYTHON3_PACKAGES_PATH=/usr/lib/python3/dist-packages -D PYTHON_DEFAULT_EXECUTABLE=/usr/bin/python3.6m \
-D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/home/lei/RK_Lib/opencv-lib \
-D BUILD_ZLIB=ON -D BUILD_JPEG=ON -D BUILD_PNG=ON -D BUILD_TIFF=ON \
-D ENABLE_CXX11=ON \
-D WITH_FFMPEG=ON -D WITH_OPENCL=ON -D WITH_GTK=OFF -D WITH_OPENGL=ON -D WITH_ZLIB=ON -D WITH_OPENMP=ON -D WITH_PNG=ON \
-D BUILD_SHARED_LIBS=OFF \
-D CMAKE_FIND_ROOT_PATH=/home/lei/RK_Lib/arm-lib \
-D BUILD_TESTS=ON \
-D FFMPEG_INCLUDE_DIRS=/home/lei/RK_Lib/arm-lib/include \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D OPENCV_FFMPEG_USE_FIND_PACKAGE=ON \
-D OPENCV_FFMPEG_SKIP_BUILD_CHECK=ON \
-D FFMPEG_DIR=/home/lei/RK_Lib/arm-lib \
..
- 执行
make -j8
- 执行
make install
问题1
执行config出现ffmpeg为NO:
解决方案
一开始确实遇到了这个问题,添加ffmpeg-config.cmake等文件后可以解决。
问题2
>
这里我没找到很好的解决方案,按照下面的方案解决需要重复修改link.txt文件若干次…慢慢来吧,这玩意确实需要耐心。
解决方案
比如进入path/opencv-3.4.16/build/modules/videoio/CMakeFiles/opencv_test_videoio.dir目录下修改link.txt,在/opt/toolchain/gcc-arm-10.2-2020.11-x86_64-aarch64-none-linux-gnu/bin/aarch64-none-linux-gnu-g++后面添加 -Wl,-rpath-link,/home/lei/RK_Lib/arm-lib/lib并保存重新执行make
反正就是找不到哪个库就-rpath到哪个库的文件夹
总结
交叉编译真麻烦…