Config.cmake Raspberry pi 交叉编译设置中缺少错误

Config.cmake missing error in Rasberry pi cross compiling setup

我正在尝试在ubuntu 16.04上构建一个交叉编译系统以缩短编译时间。

我已经安装了工具并从 raspberry pi 复制了库,一个简单的“Hello world”示例绝对可以很好地工作。

当我尝试用cmake和opencv构建程序时出现问题。显然它在主机系统中找不到 OpenCVConfig.cmake,因为它仅存在于以下文件夹中的 rpi 上:

/usr/local/lib/cmake/opencv4$

我可以只复制 /usr/local/lib/cmake 文件夹到主机并手动修改库安装路径吗?或者我应该怎么做?

错误信息:

找不到“OpenCV”提供的包配置文件 以下姓名:

OpenCVConfig.cmake
opencv-config.cmake

将“OpenCV”的安装前缀添加到CMAKE_PREFIX_PATH或设置 “OpenCV_DIR”到包含上述文件之一的目录。如果“OpenCV” 提供单独的开发包或SDK,确保已经 已安装。

我的CMakeLists.txt:

set(CMAKE_BUILD_TYPE Release)
SET(CMAKE_SYSTEM_NAME Linux)
SET(CMAKE_SYSTEM_VERSION 1)


SET(CMAKE_C_COMPILER $ENV{HOME}/raspi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin/arm-linux-gnueabihf-gcc)
SET(CMAKE_CXX_COMPILER $ENV{HOME}/raspi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin/arm-linux-gnueabihf-g++)

# Where is the target environment
SET(CMAKE_FIND_ROOT_PATH $ENV{HOME}/raspi/sysroot)
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --sysroot=${CMAKE_FIND_ROOT_PATH}")
SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} --sysroot=${CMAKE_FIND_ROOT_PATH}")
SET(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} --sysroot=${CMAKE_FIND_ROOT_PATH}")

# Search for programs only in the build host directories
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)

# Search for libraries and headers only in the target directories
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)


set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_FLAGS_RELEASE "-O3 -Wall -fopenmp")

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fopenmp")

find_package( OpenCV REQUIRED )

find_library(LIB_RT rt)

list(APPEND LINK_LIBS 
  ${OpenCV_LIBS}
)


include_directories(
  ${OpenCV_INCLUDE_DIRS}
)


set(SOURCE opencvdemo.cpp)

add_executable(vc_opencv_demo ${SOURCE})
target_link_libraries(vc_opencv_demo ${LIB_RT} ${LINK_LIBS})

在您的方案中,您已将软件包安装在自定义位置。

您可以尝试以下方法(来自 the cmake manual):

The default search order is designed to be most-specific to least-specific for common use cases. Projects may override the order by simply calling the command multiple times and using the NO_* options:

find_package (<package> PATHS paths... NO_DEFAULT_PATH)

我建议您一般使用一些 cmake 选项进行 linux 交叉编译:

set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR arm)

set(CMAKE_SYSROOT /home/devel/rasp-pi-rootfs)
set(CMAKE_STAGING_PREFIX /home/devel/stage)

set(tools /home/devel/gcc-4.7-linaro-rpi-gnueabihf)
set(CMAKE_C_COMPILER ${tools}/bin/arm-linux-gnueabihf-gcc)
set(CMAKE_CXX_COMPILER ${tools}/bin/arm-linux-gnueabihf-g++)

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)

请参阅 link 解释选项的地方

我的一个简单解决方案是添加以下文件(或编写您自己的文件): https://github.com/opencv/opencv/blob/2f4e38c8313ff313de7c41141d56d945d91f47cf/cmake/OpenCVConfig.cmake

到您的 CmakeLists.txt 所在的文件夹“cmake-modules”,例如:

cmake-modules/FindOpenCV.cmake

那请加

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake-modules)

在调用 find_package(OpenCV)

之前

另一个快速修复的想法是使用系统变量来表达,在哪里可以找到不同 cmake 目标的 opencv 库 在调用 cmake 之前从命令行调用以下命令:

export CMAKE_PREFIX_PATH=opencv_installation_folder/build

export OpenCV_DIR=opencv_installation_folder/build

或者在你的 cmake 文件中写入

set (OpenCV_DIR /home/cmake/opencv/compiled) #change the path to match your complied directory of opencv

我认为您的交叉编译配置中缺少 set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)

将所有交叉编译选项放入所谓的工具链文件中,然后使用 cmake -DCMAKE_TOOLCHAIN_FILE=/path/to/toolchainfile/raspberrypi.cmake ..

也是一种很好的做法

另一种选择是从源代码构建 OpenCV,因此您无需担心它是否已打包到您的工具链中。将 find_package 替换为:

include(FetchContent)
FetchContent_Declare(
  opencv
  GIT_REPOSITORY https://github.com/opencv/opencv.git
  GIT_TAG 4.4.0)

FetchContent_GetProperties(opencv)
if(NOT opencv_POPULATED)
  FetchContent_Populate(opencv)
  add_subdirectory(${opencv_SOURCE_DIR} ${opencv_BINARY_DIR})
endif()