5

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?

2 Answers 2

2

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
}
Sign up to request clarification or add additional context in comments.

Comments

0

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.

3 Comments

It didn't make any difference I'm afraid.
The error is stating compile_commands.json yet your code is from c_cpp_properties.json. It could be that the second is automatically generated from the first, and there is no way for me to verify that. Make absolutely sure the include path is updated in the correct place.
Errors of such types have been reported to C++ VSCode Intellisense issue tracker multiple times and it seems to be an issue with the version of the extension being used. If your current version of intellisense is troubling, try installing an older version and use. Once a more stable version is released, update the extension.

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.