编程语言
首页 > 编程语言> > c-如何通过其他模块交叉编译Raspberry Pi的OpenCV(highgui …)

c-如何通过其他模块交叉编译Raspberry Pi的OpenCV(highgui …)

作者:互联网

我的目标是在Ubuntu 16.04主机上使用CMake对Raspberry Pi的OpenCV进行交叉编译.问题位于CMake工具链文件中,该文件无法正确处理编译过程.一旦pkg-config生效,就会出现许多“致命错误:没有这样的文件或目录”错误(例如“致命错误:gtk / gtk.h:没有这样的文件或目录).

我发现带有包含丢失文件的目录的其他“ -isystem”标志似乎在某种程度上有所帮助,但是我怀疑这是否是解决这些错误的正确方法.

只要注释掉“ PKG_CONFIG”-东西,工具链文件就可以工作.这将导致pkg-config找不到“ libgtk2.0”,这是OpenCV的highgui模块所需的,并且在没有highgui支持的情况下进行构建.

工具链的一些想法来自这里:

> Cmake cross compile cant find library
> Raspberry Pi Beginners Guide
> Cross compile opencv project on Ubuntu for Raspberry Pi

构建工具链

#!/bin/bash

# apt -y install lib32z1 libstdc++6:i386 git qemu-user proot cmake pkg-config

MOUNT_DIR=/mnt/rasp-pi-rootfs
TEMP_DIR=/tmp/build
RASPBIAN_IMG_ZIP=raspbian-jessie.zip
TOOLCHAIN=gcc-linaro-arm-linux-gnueabihf-raspbian-x64
CMAKE_TOOLCHAIN_FILE=rpi-toolchain.cmake
############################################################################
RASPBIAN_IMG=${TEMP_DIR}/$(unzip -Z1 $RASPBIAN_IMG_ZIP)

rm -rf $TEMP_DIR
mkdir $TEMP_DIR
cd $TEMP_DIR

#unzip raspbian-img
unzip $RASPBIAN_IMG_ZIP

#resize raspbian-img
LOOP_DEVICE=$(losetup -f)
RASPBIAN_PARTITION_BOOT=${LOOP_DEVICE}p1
RASPBIAN_PARTITION_SYSTEM=${LOOP_DEVICE}p2
truncate -s +500M $RASPBIAN_IMG
losetup $LOOP_DEVICE $RASPBIAN_IMG
parted $LOOP_DEVICE resizepart 2 100%
#wait until auto-mounted
umount $RASPBIAN_PARTITION_BOOT
umount $RASPBIAN_PARTITION_SYSTEM
e2fsck -f $RASPBIAN_PARTITION_SYSTEM
resize2fs $RASPBIAN_PARTITION_SYSTEM

#mount raspbian-img
mkdir -p $MOUNT_DIR
mount $RASPBIAN_PARTITION_SYSTEM $MOUNT_DIR

#fix absolute links
proot -q qemu-arm -S $MOUNT_DIR<<EOF
apt update

apt -y install libjpeg-dev libtiff5-dev libjasper-dev libpng12-dev \
libavcodec-dev libavformat-dev libswscale-dev libv4l-dev \
libxvidcore-dev libx264-dev \
libgtk2.0-dev libatlas-base-dev gfortran

apt -y install symlinks
symlinks -cr /usr/lib
EOF

wget -O opencv.zip https://codeload.github.com/Itseez/opencv/zip/2.4.13
unzip opencv.zip
cd opencv-*
mkdir build
cd build

cmake -D CMAKE_TOOLCHAIN_FILE=$CMAKE_TOOLCHAIN_FILE -D PIROOT=$MOUNT_DIR -D TOOLROOT=$TOOLCHAIN -D CMAKE_BUILD_TYPE=Release -D WITH_V4L=ON -D WITH_OPENGL=ON ..
make -j $(nproc)
make install

sync; umount $MOUNT_DIR
losetup -d $LOOP_DEVICE

rpi-toolchain.cmake

set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_VERSION 1)
set(CMAKE_SYSTEM_PROCESSOR arm)

set(CMAKE_C_COMPILER ${TOOLROOT}/bin/arm-linux-gnueabihf-gcc)
set(CMAKE_CXX_COMPILER ${TOOLROOT}/bin/arm-linux-gnueabihf-g++)

set(CMAKE_SYSROOT ${PIROOT})
set(CMAKE_FIND_ROOT_PATH ${PIROOT})

unset(ENV{PKG_CONFIG_DIR})
set(ENV{PKG_CONFIG_LIBDIR} "$ENV{PKG_CONFIG_LIBDIR}:${PIROOT}/usr/lib/arm-linux-gnueabihf/pkgconfig:${PIROOT}/usr/share/pkgconfig:${PIROOT}/usr/lib/pkgconfig")
set(ENV{PKG_CONFIG_SYSROOT_DIR} "${PIROOT}")

set(FLAGS "-isystem ${PIROOT}/usr/include/arm-linux-gnueabihf")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${FLAGS}" CACHE STRING "" FORCE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${FLAGS}" CACHE STRING "" FORCE)

set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)

set(CMAKE_INSTALL_PREFIX ${PIROOT}/usr/local)

解决方法:

得到它了.问题是rpi-toolchain.cmake文件中的PKG_CONFIG-vars设置,这不起作用.必须通过export将它们添加到环境中:

...
export PKG_CONFIG_SYSROOT_DIR=${MOUNT_DIR}
export PKG_CONFIG_LIBDIR=${MOUNT_DIR}/usr/lib/arm-linux-gnueabihf/pkgconfig:${MOUNT_DIR}/usr/share/pkgconfig:${MOUNT_DIR}/usr/lib/pkgconfig
export PKG_CONFIG_DIR=

cmake -D CMAKE_TOOLCHAIN_FILE=$CMAKE_TOOLCHAIN_FILE -D PIROOT=$MOUNT_DIR -D TOOLROOT=$TOOLCHAIN -D CMAKE_BUILD_TYPE=Release -D WITH_V4L=ON -D WITH_OPENGL=ON ..
make -j $(nproc)

标签:opencv,raspberry-pi,cross-compiling,linux,c-4
来源: https://codeday.me/bug/20191112/2024188.html