Daniel Jonsson
Daniel Jonsson

Reputation: 3933

Visual Studio Code "cannot open source file" error when including OpenCV

I'm using Visual Studio Code to develop a C++ project, and I have OpenCV installed in a custom location. However, it complains with the following error when I try to include header files from OpenCV:

#include errors detected. Consider updating your compile_commands.json or includePath. Squiggles are disabled for this translation unit (/home/.../dev/communication-module/modules/.../.../src/....cpp). C/C++(1696)
cannot open source file "opencv2/core/mat.hpp" C/C++(1696)

Here is a screenshot of the same error:

enter image description here

My .vscode/c_cpp_properties.json file looks as follows:

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "/opt/sdk/sysroots/corei7-64-poky-linux/usr/include/opencv2",
                "/opt/sdk/sysroots/corei7-64-poky-linux/usr/include/opencv2/core",
                "${workspaceFolder}/**"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x64",
            "compileCommands": "${workspaceFolder}/build/compile_commands.json",
            "browse": {
                "path": [
                    "/opt/sdk/sysroots/corei7-64-poky-linux/usr/include/opencv2",
                    "/opt/sdk/sysroots/corei7-64-poky-linux/usr/include/opencv2/core",
                    "${workspaceFolder}"
                ],
                "limitSymbolsToIncludedHeaders": true,
                "databaseFilename": ""
            }
        }
    ],
    "version": 4
}

And the mat.hpp file is clearly there:

$ ls /opt/sdk/sysroots/corei7-64-poky-linux/usr/include/opencv2/core | grep mat.hpp
mat.hpp

Still, Visual Studio Code does not pick it up. Why is that? What else should I change for Visual Studio Code to find my OpenCV header files?

Upvotes: 5

Views: 15184

Answers (2)

Daniel Jonsson
Daniel Jonsson

Reputation: 3933

The offending line was apparently:

            "compileCommands": "${workspaceFolder}/build/compile_commands.json",

After having removed it, it now works. My c_cpp_properties.json config now looks as follows:

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "/opt/sdk/sysroots/corei7-64-poky-linux/usr/include",
                "${workspaceFolder}/**"
            ],
            "defines": [],
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x64"
        }
    ],
    "version": 4
}

Upvotes: 2

Arne J
Arne J

Reputation: 415

Your include path /opt/sdk/sysroots/corei7-64-poky-linux/usr/include/opencv2 should end with /include .

When you type #include <opencv2/core/mat.hpp> the compiler will search try out /opt/sdk/sysroots/corei7-64-poky-linux/usr/include/opencv2/opencv2/core/mat.hpp which will obviously not work.

Upvotes: 0

Related Questions