0

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

6
  • If you do ls -l /usr/include/opencv4/opencv2/core.hpp, do you see the file or get an error? If it's not there: sudo apt install libopencv-dev
    – Ted Lyngmo
    Commented Jun 7, 2021 at 16:43
  • Yes, the file exists as I showed in the question along other files -rw-r--r-- 1 root root 151612 Dec 20 2019 /usr/include/opencv4/opencv2/core.hpp
    – wbadry
    Commented Jun 7, 2021 at 17:18
  • Ok, I could see the pictures. Perhaps changing to the C++ compiler instead of using the C compiler changes something?
    – Ted Lyngmo
    Commented Jun 7, 2021 at 17:24
  • @TedLyngmo I really tried cpp, gcc and g++. I guess I shall use normal cmake burden to compile it
    – wbadry
    Commented Jun 7, 2021 at 23:35
  • 1
    cpp isn't a compiler in the normal sense, it's a pre-processor. gcc is what you should use for C programs and g++ is what should be used for C++ programs.
    – Ted Lyngmo
    Commented Jun 8, 2021 at 5:05

1 Answer 1

4

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`"

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.