简体   繁体   中英

Issue building opencv using FetchContent cmake command

I really need your help because I really don't understand what I am doing wrong.

Using the very simple attached CMakeLists.txt, I got the following error (a lot of times):

CMake Error in build/_deps/opencv-src/modules/core/CMakeLists.txt:
  Target "opencv_core" INTERFACE_INCLUDE_DIRECTORIES property contains path:

    "/home/myuser/tmp/build"

  which is prefixed in the build directory.

Can you help me fix it?

Thank you for the help.

The CMakeLists.txt I am using:

cmake_minimum_required(VERSION 3.8 FATAL_ERROR)

project(TEST_PROJECT)

set(CMAKE_CXX_STANDARD 14)

if (CMAKE_VERSION VERSION_GREATER_EQUAL "3.24.0")
        cmake_policy(SET CMP0135 NEW)
endif ()

include(FetchContent)

FetchContent_Declare(
        opencv
        GIT_REPOSITORY https://github.com/opencv/opencv.git
        GIT_TAG 4.6.0
        GIT_SHALLOW TRUE
        GIT_PROGRESS TRUE
)
FetchContent_MakeAvailable(opencv)
set(OpenCV_DIR ${CMAKE_CURRENT_BINARY_DIR})
find_package(OpenCV REQUIRED)

Tried the same on a Windows system - with no success either.

In the end, I installed OpenCV outside of the build and source tree. Then used only find_package in the CMakeList.txt.

I had problems passing varibales to the OpenCV build when using FetchContent too. Eg for using TBB or CUDA.

As @sweenish guessed, the problem was the OpenCV_DIR variable definition. So I remove this line and add the OVERRIDE_FIND_PACKAGE in the FetchContent_Declare call. And now it works fine on both systems, Linux and Windows.

My CMakeLists.txt now looks like:

cmake_minimum_required(VERSION 3.8 FATAL_ERROR)

project(TEST_PROJECT)

set(CMAKE_CXX_STANDARD 14)

if (CMAKE_VERSION VERSION_GREATER_EQUAL "3.24.0")
        cmake_policy(SET CMP0135 NEW)
endif ()

include(FetchContent)

FetchContent_Declare(
        opencv
        GIT_REPOSITORY https://github.com/opencv/opencv.git
        GIT_TAG 4.6.0
        GIT_SHALLOW TRUE
        GIT_PROGRESS TRUE
        OVERRIDE_FIND_PACKAGE
)
FetchContent_MakeAvailable(opencv)
find_package(OpenCV REQUIRED)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM