wbadry
wbadry

Reputation: 925

fatal error: opencv2/core.hpp: No such file or directory Ubuntu and Visual Studio Code

I have the opencv installed in /usr/include/opencv4 folder

opencv location

I configured the VSCode as it should be, and the IntelliSense can detect the library.

enter image description here

The configuration should be correct (I guess)

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**",
                "/usr/include/opencv4/**"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "gnu17",
            "cppStandard": "gnu++14",
            "intelliSenseMode": "linux-gcc-x64"
        }
    ],
    "version": 4
}

However, I get an error:

fatal error: opencv2/core.hpp: No such file or directory
    2 | #include <opencv2/core.hpp>
      |          ^~~~~~~~~~~~~~~~~~
compilation terminated.

Am I missing something? Thanks

Upvotes: 0

Views: 8401

Answers (1)

wbadry
wbadry

Reputation: 925

After a couple of days, I was able to find out the solution.

It was to add the necessary flags to the build task.

I posted the full sample on GitHub. Hopefully, it will help someone in the future.

The task.json should be

    {
    "version": "2.0.0",
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++ build active file",
            "command": "/usr/bin/g++",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}",
                "`pkg-config",
                "--cflags",
                "--libs",
                "opencv4`"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "compiler: /usr/bin/g++"
        }
    ]
}

The missing was to add these arguments

"`pkg-config",
"--cflags",
"--libs",
"opencv4`"

Upvotes: 4

Related Questions