1

I'm pretty new to using CMake to build/compile OpenCV projects in Visual Studio Code.

From my understanding so far, this is the 'typical' CMake file 'format' if I want to compile a OpenCV project

cmake_minimum_required(VERSION 3.0.0)

project(OpenCV_Hello_World VERSION 0.1.0 LANGUAGES C CXX)

# Specify the C++ standard
set(CMAKE_CXX_STANDARD 11)

# Find OpenCV package
find_package(OpenCV REQUIRED)

# Include OpenCV headers
include_directories(${OpenCV_INCLUDE_DIRS})

# Create the executable
add_executable(OpenCV_Hello_World main.cpp)

# Link OpenCV libraries
target_link_libraries(OpenCV_Hello_World PRIVATE ${OpenCV_LIBS})

In my main directory, I have :

  • main.cpp
  • CMakelists.txt
  • build folder to hold all the files that get generated from the CMake file

With the CMake file listed above, when I use cmake -G "Unix Makefiles" .. and make to build/compile my main.cpp, I get the following error:

CMake Error at E:/Coding Folder/OpenCV Folder/build/win-install/x64/vc16/lib/OpenCVConfig.cmake:290 (get_target_property):
  get_target_property() called with non-existent target "opencv_world".

But if I were to specify where my OpenCV lib files are at with the set() command, I'm able to compile/build everything just fine.

Here's the CMake file that I'm currently using


cmake_minimum_required(VERSION 3.0.0)

project(OpenCV_Hello_World VERSION 0.1.0 LANGUAGES C CXX)

# Set the OpenCV directory
#set(OpenCV_DIR " INSERT PATH TO OPENCV LIB HERE ")

# Specify the C++ standard
set(CMAKE_CXX_STANDARD 11)

# Find OpenCV package
find_package(OpenCV REQUIRED)

# Include OpenCV headers
include_directories(${OpenCV_INCLUDE_DIRS})

# Create the executable
add_executable(OpenCV_Hello_World main.cpp)

# Link OpenCV libraries
target_link_libraries(OpenCV_Hello_World PRIVATE ${OpenCV_LIBS})

I was under the heavy impression that the find_package(OpenCV REQUIRED) command would 'find' my OpenCV files and I'm confused on why that's not the case.

3
  • File <...>/win-install/x64/vc16/lib/OpenCVConfig.cmake describes installation of OpenCV for Visual Studio. It is not compatible with Unix Makefiles generator, which configures the project for Linux (you run the configure under WSL, don't you?). Commented Aug 27, 2023 at 21:42
  • To be fully transparent with you, the only reason I use cmake -G "Unix Makefiles" .. and the make command is purely so I don't have to cd into the Release folder if I used cmake -DCMAKE_BUILD_TYPE=Release .. and cmake --build . --config Release to build and compile with my Visual Studio configuration. But to answer your question if I'm "Running the configure under WSL" I'm using a Powershell terminal in Visual Studio Code Commented Aug 27, 2023 at 22:34
  • What compiler do you use when configure the project? Normally, CMake prints the compiler at the beginning of the configuration output. Commented Aug 28, 2023 at 6:28

0

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.