Vivek Kumar
Vivek Kumar

Reputation: 11

VSCode CMake Not Linking Package

I'm trying to create a project using OpenCV in VS Code and I'm having an issue where the included header is not being detected despite no issues occurring with CMake. My CMake code is as follows:

cmake_minimum_required(VERSION 3.0.0)
project(ASCII VERSION 0.1.0)

include(CTest)
enable_testing()

find_package( OpenCV REQUIRED PATHS "C:/opencv")
include_directories(${OpenCV_INCLUDE_DIRS})
add_executable( ASCII main.cpp )
target_link_libraries( ASCII ${OpenCV_LIBS} )

set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)

My main.cpp file is as follows:

#include <stdio.h>
#include <opencv2/opencv.hpp>

using namespace cv;
int main(int argc, char** argv )
{

    Mat image;
    image = imread("C:/Users/Vivek/lenna.png");
    if ( !image.data )
    {
        printf("No image data \n");
        return -1;
    }
    namedWindow("Display Image", WINDOW_AUTOSIZE );
    imshow("Display Image", image);
    waitKey(0);
    return 0;
}

I keep getting the error:

main.cpp(2): fatal error C1083: Cannot open include file: 'opencv2/opencv.hpp': No such file or directory

This is my first time working with CMake, so I'm not entirely sure what the issue is. It doesn't throw any errors with CMake itself, only when I try to run main.

Upvotes: 0

Views: 739

Answers (1)

Matti
Matti

Reputation: 98

It seems like, that the include_directories is pointing to unexpected content.

You could add this after the include_directories call in the CMakeLists.txt:

message(
"OpenCV package:
    found: ${OpenCV_FOUND}
    include dir: ${OpenCV_INCLUDE_DIRS}
    libraries: ${OpenCV_LIBS}"
)

and look at the printed include dir: . The directory it is pointing to, should contain a structure like opencv2\opencv.hpp.

P.S.: I tried your code with the OpenCV – 4.6.0 release with the windows-release package and the build worked.

Upvotes: 1

Related Questions