最近由于工作需要,要交叉编译 opencv 和 ffmpeg 生成静态库。
配置:opencv 3.4.12 ,ffmpeg 4.2.9,x264。
- 创建文件夹,把 x264、ffmpeg、opencv 压缩包全放进去。
- 创建安装文件夹,例如 /home/user/opencv
安装依赖项
sudo apt-get install pkg-config libgtk2.0-dev libavcodec-dev libavformat-dev libswscale-dev
交叉编译 x264
由于 ffmepg 依赖于 x264,所以需要先编译 x264。解压进入文件夹:
cd /home/user/work/x264-master
同时生成静态库和动态库:
CC=aarch64-linux-gnu-gcc ./configure --enable-shared --enable-static --host=aarch64-linux-gnu --disable-asm --prefix=/home/user/opencv
make
make install
编译 ffmpeg
解压入文件
d /home/user/work/ffmpeg
./configure --prefix=/home/user/opencv --enable-static --disable-static --enable-gpl --enable-cross-compile --arch=arm64 --disable-stripping --target-os=linux --enable-libx264 --cc=aarch64-linux-gnu-gcc --enable-swscale --extra-cflags=-I/home/user/opencv/include/ --extra-ldflags=-L/home/user/opencv/lib/
make
make install
编译 opencv
解压进入文件夹:
cd /home/user/work/opc
#创建build 文件夹
设置 pkg 环境变量:
sudo gedit /etc/profile
#文件末尾添加
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/home/user/opencv/lib/pkgconfig
source /etc/profile
运行 cmake-gui
1) 设置源码和编译目录:
/home/user/work/opencv3.4.12 /home/user/work/opencv3.4.12/build
2)点击 configure 配置编译链
Operating system:Linux
c: gcc目录 c++:g++目录
targetroot:gcc,g++的上一级目录。
3)CMAKE_INSTALL_PRIFIX:/home/user/opencv
4)选择必要的库,BUILD_ZLIB 、![在这里插入图片描述](https://img-blog.csdnimg.cn/66aa014503f4450c872b3483d8a27fed.png
BUILD_JPEG、BUILD_PNG、BUILD_JASPER、BUILD_TIFF。(按需选择)
5)添加 OPENCV_ENABLE_PKG_CONFIG 并设置为true
6)BUILD_SHARED_LIBS去掉
7)去掉一些不需要的,这个可以参考其他博客
8)点击Advanced ,在CMAKE_EXE_LINKER_FLAGS 选项中设为-lpthread -lrt -ldl -lm
9)点击 Configure、Generate。
注:如果 FFMPEG对应部分为 no,可以看一下 相关的路径是否识别到,可以手动设置。
make
make install
最后整理所有静态库即可,x264 动态库可以删除。
运行报错 undefined reference to avcodec_encode_video2' 编译成功后,运行测试程序报错: ../lib/opencv/libopencv_videoio.a(cap_ffmpeg.cpp.o): In function icv_av_write_frame_FFMPEG(AVFormatContext*, AVStream*, unsigned char*, unsigned int, AVFrame*) [clone .isra.5]': cap_ffmpeg.cpp:(.text._ZL25icv_av_write_frame_FFMPEGP15AVFormatContextP8AVStreamPhjP7AVFrame.isra.5+0x24): undefined reference to av_init_packet' cap_ffmpeg.cpp:(.text._ZL25icv_av_write_frame_FFMPEGP15AVFormatContextP8AVStreamPhjP7AVFrame.isra.5+0x44): undefined reference to avcodec_encode_video2' cap_ffmpeg.cpp:(.text._ZL25icv_av_write_frame_FFMPEGP15AVFormatContextP8AVStreamPhjP7AVFrame.isra.5+0x74): undefined reference to av_rescale_q' cap_ffmpeg.cpp:(.text._ZL25icv_av_write_frame_FFMPEGP15AVFormatContextP8AVStreamPhjP7AVFrame.isra.5+0x94): undefined reference to av_rescale_q' cap_ffmpeg.cpp:(.text._ZL25icv_av_write_frame_FFMPEGP15AVFormatContextP8AVStreamPhjP7AVFrame.isra.5+0xac): undefined reference to av_rescale_q' cap_ffmpeg.cpp:(.text._ZL25icv_av_write_frame_FFMPEGP15AVFormatContextP8AVStreamPhjP7AVFrame.isra.5+0xc4): undefined reference to av_write_frame' cap_ffmpeg.cpp:(.text._ZL25icv_av_write_frame_FFMPEGP15AVFormatContextP8AVStreamPhjP7AVFrame.isra.5+0xd0): undefined reference to av_packet_unref' ../lib/opencv/libopencv_videoio.a(cap_ffmpeg.cpp.o): In function
CvCapture_FFMPEG::init()':
但是已经正确连接了库,已经包含目录,网上的解决方法都没有,在 ta将rget_link_libraries 中 OPENCV_LIB 放在 FFMPEG_LIBRARIES之前即可。
target_link_libraries(project ${OPENCV_LIB} ${FFMPEG_LIBRARIES})
参考:https://blog.csdn.net/qq_39660930/article/details/130245983