1

I have these files that try to build a simple application with OpenCV in Linux:

cmake_minimum_required(VERSION 3.29)
project(opencv_app)

# Enable C++17
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Enable vcpkg manifest mode
if(DEFINED ENV{VCPKG_ROOT})
    set(CMAKE_TOOLCHAIN_FILE "$ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake"
        CACHE STRING "")

    # Set a suitable triplet for Linux (adjust if using a different architecture)
    set(VCPKG_TARGET_TRIPLET "x64-linux" CACHE STRING "")
endif()

message(STATUS "Using vcpkg toolchain file: ${CMAKE_TOOLCHAIN_FILE}")
message(STATUS "Using vcpkg triplet: ${VCPKG_TARGET_TRIPLET}")

# Add executable
add_executable(opencv_app main.cpp)

# Find OpenCV package via vcpkg
find_package(OpenCV REQUIRED)
target_link_libraries(opencv_app PRIVATE ${OpenCV_LIBS})
target_include_directories(opencv_app PRIVATE ${OpenCV_INCLUDE_DIRS})

and vcpkg.json:

{
  "name": "opencv_app",
  "version": "0.1.0",
  "dependencies": [
    {
      "name": "opencv",
      "default-features": false,
      "features": [ "jpeg" ]
    }
  ]
}

and I am trying to build it using this command:

cmake -B build -S .

But I am getting this error:

cmake -B build -S .
-- The C compiler identification is GNU 13.3.0
-- The CXX compiler identification is GNU 13.3.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Using vcpkg toolchain file: /media/r/Data/locals/vcpkg/scripts/buildsystems/vcpkg.cmake
-- Using vcpkg triplet: x64-linux
CMake Error at CMakeLists.txt:24 (find_package):
  By not providing "FindOpenCV.cmake" in CMAKE_MODULE_PATH this project has
  asked CMake to find a package configuration file provided by "OpenCV", but
  CMake did not find one.

  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.


-- Configuring incomplete, errors occurred!

What is the problem and how to fix it?

I am using CMake version 4.1.0 and Linux 22.04, and the latest version of vcpkg (just installed it last week)

9
  • This is possibly a duplicate. See previous Commented Aug 22 at 18:36
  • 1
    @SixEight The main difference is that in my case, I am using vcpkg in manifest mode to manage packages. Commented Aug 22 at 19:12
  • I also don't think its a proper duplicate for this exact problem. Do either of those files exist? I have spent a lot of time with vcpkg on linux recently but not with OpenCV. Commented Aug 22 at 19:20
  • Well no, @mans, that's not really a distinction from the previous question. It doesn't matter how you are managing the package, only that that mechanism is not providing the needed CMake script in any of the places that your CMake is looking for it. The solution is to do whatever more is needed to provide such a script. Possibly there's an additional package you could install, or possibly you need to find a suitable script (or write one) and install it manually. Or possibly you just need to point CMake to a non-default place to find it. Commented Aug 22 at 19:21
  • @drescherjm I was using VCPKG in manifest mode and never had this problem. It seems that there is a change in VCPKG that breaks it. Apparently, VCPKG is not reading vcpkg.json and is not installing OpenCV, so I am getting the error that OpenCV is not found. Commented Aug 22 at 19:40

2 Answers 2

3

The problem is the placement of

# Enable vcpkg manifest mode
if(DEFINED ENV{VCPKG_ROOT})
    set(CMAKE_TOOLCHAIN_FILE "$ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake"
        CACHE STRING "")

    # Set a suitable triplet for Linux (adjust if using a different architecture)
    set(VCPKG_TARGET_TRIPLET "x64-linux" CACHE STRING "")
endif()

It should be before the project statement, like below:

cmake_minimum_required(VERSION 3.29)
# Enable vcpkg manifest mode
if(DEFINED ENV{VCPKG_ROOT})
    set(CMAKE_TOOLCHAIN_FILE "$ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake"
        CACHE STRING "")

    # Set a suitable triplet for Linux (adjust if using a different architecture)
    set(VCPKG_TARGET_TRIPLET "x64-linux" CACHE STRING "")
endif()
project(opencv_app)

# Enable C++17
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)



message(STATUS "Using vcpkg toolchain file: ${CMAKE_TOOLCHAIN_FILE}")
message(STATUS "Using vcpkg triplet: ${VCPKG_TARGET_TRIPLET}")

# Add executable
add_executable(opencv_app main.cpp)

# Find OpenCV package via vcpkg
find_package(OpenCV REQUIRED)
target_link_libraries(opencv_app PRIVATE ${OpenCV_LIBS})
target_include_directories(opencv_app PRIVATE ${OpenCV_INCLUDE_DIRS})

Now it works properly!

Sign up to request clarification or add additional context in comments.

2 Comments

is there a specific reason why this must be the case? usually the project statement is he first thing one expects to see in a cmakelist file.
I believe that the toolchain file is processed in the project() so if you set CMAKE_TOOLCHAIN_FILE after the project function executes it's already too late.
1

While @mans' answer works (I assume) - that is not, IMHO, the fundamental problem.

The problem is with how you're using CMake, and what you're putting into your CMakeLists.txt.

A project's CMakeLists.txt should declare what the project needs for a build to occur; it should avoid specifying how those needs are to be met. Specifically:

  • Avoid setting variables to reflect a specific build platform.
  • Do not presume to obtain dependencies using vcpkg.
  • Do not presume to locate a toolchain file at a specific location, and in fact,
  • Avoid setting a toolchain file yourself, at all.

Instead - detect which platform you're on, and fail if your project doesn't support it. And more significantly - let the user invoking cmake for your project be the one setting a toolchain file.

If you were to do that, you would also automatically set your toolchain, and made other vcpkg-related settings, before your project() statement. It is the "imperative" presumption about the surroundings rather than the "declarative" specification of needs that your CMakeLists.txt exhibits that opened you up for this bug.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.