Wednesday, February 2, 2022

[SOLVED] Config.cmake missing error in Rasberry pi cross compiling setup

Issue

I am trying to build a cross compiling system on ubuntu 16.04 to shorten the compile time.

I have installed the tools and copied libs from the raspberry pi and definitely a simple "Hello world" example works very well.

The problem occurred when I tried to build a program with cmake and opencv. Obviously it does not find OpenCVConfig.cmake in the host system as it only exists on rpi in the following folder:

/usr/local/lib/cmake/opencv4$

Can I just copy /usr/local/lib/cmake folder to host computer and manually modify the library install path or how should I do it?

Error message:

Could not find a package configuration file provided by "OpenCV" with any of the following names:

OpenCVConfig.cmake
opencv-config.cmake

Add the installation prefix of "OpenCV" to CMAKE_PREFIX_PATH or set "OpenCV_DIR" to a directory containing one of the above files. If "OpenCV" provides a separate development package or SDK, be sure it has been installed.

My 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})

Solution

I would suggest you to use in general some cmake option for linux cross compiling:

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)

see the link where options are explained

A simple solution for me was to add following file (or write your own): https://github.com/opencv/opencv/blob/2f4e38c8313ff313de7c41141d56d945d91f47cf/cmake/OpenCVConfig.cmake

to a folder "cmake-modules" where your CmakeLists.txt is located e.g:

cmake-modules/FindOpenCV.cmake

Then please add

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

before calling find_package(OpenCV)

Another idea for a quick fix is to use the system variable to express, where to find the opencv lib for different cmake targets calling the follwing command from commadline before calling cmake :

export CMAKE_PREFIX_PATH=opencv_installation_folder/build

or

export OpenCV_DIR=opencv_installation_folder/build

or write inside your cmake file

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



Answered By - t2solve
Answer Checked By - Marie Seifert (WPSolving Admin)