【问题标题】:Visual Studio Code "cannot open source file" error when including OpenCV包含 OpenCV 时的 Visual Studio Code“无法打开源文件”错误
【发布时间】:2020-03-15 22:55:06
【问题描述】:

我正在使用 Visual Studio Code 开发一个 C++ 项目,并且我在自定义位置安装了 OpenCV。但是,当我尝试包含来自 OpenCV 的头文件时,它会抱怨以下错误:

#include 检测到错误。考虑更新您的 compile_commands.json 或 includePath。此翻译单元 (/home/.../dev/communication-module/modules/.../.../src/....cpp) 禁用了曲线。 C/C++(1696)
无法打开源文件“opencv2/core/mat.hpp”C/C++(1696)

这是相同错误的屏幕截图:

我的.vscode/c_cpp_properties.json 文件如下所示:

{
    "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
}

mat.hpp 文件显然在那里:

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

不过,Visual Studio Code 并没有接受它。这是为什么?为了让 Visual Studio Code 找到我的 OpenCV 头文件,我还应该进行哪些更改?

【问题讨论】:

    标签: c++ visual-studio-code


    【解决方案1】:

    您的包含路径 /opt/sdk/sysroots/corei7-64-poky-linux/usr/include/opencv2 应以 /include 结尾。

    当您输入#include <opencv2/core/mat.hpp> 时,编译器将搜索尝试/opt/sdk/sysroots/corei7-64-poky-linux/usr/include/opencv2/opencv2/core/mat.hpp,这显然不起作用。

    【讨论】:

    • 恐怕没什么区别。
    • 错误是compile_commands.json,但您的代码来自c_cpp_properties.json。可能第二个是从第一个自动生成的,我无法验证这一点。确保在正确的位置更新包含路径。
    • 此类错误已多次报告给 C++ VSCode Intellisense 问题跟踪器,这似乎是正在使用的扩展版本的问题。如果您当前的智能感知版本有问题,请尝试安装旧版本并使用。一旦发布了更稳定的版本,请更新扩展。
    【解决方案2】:

    明显的问题是:

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

    删除它后,它现在可以工作了。我的c_cpp_properties.json 配置现在如下所示:

    {
        "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
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-29
      • 2016-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-17
      • 2017-10-28
      • 1970-01-01
      相关资源
      最近更新 更多