【问题标题】:fatal error: opencv2/core.hpp: No such file or directory Ubuntu and Visual Studio Code致命错误:opencv2/core.hpp:没有这样的文件或目录 Ubuntu 和 Visual Studio Code
【发布时间】:2021-08-24 18:10:11
【问题描述】:

我在/usr/include/opencv4 文件夹中安装了opencv

我按照应有的方式配置了 VSCode,IntelliSense 可以检测到库。

配置应该是正确的(我猜)

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

但是,我得到一个错误:

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

我错过了什么吗?谢谢

【问题讨论】:

  • 如果您执行ls -l /usr/include/opencv4/opencv2/core.hpp,您会看到文件还是收到错误消息?如果不存在:sudo apt install libopencv-dev
  • 是的,正如我在问题中所显示的那样,该文件存在于其他文件-rw-r--r-- 1 root root 151612 Dec 20 2019 /usr/include/opencv4/opencv2/core.hpp
  • 好的,我可以看到图片了。也许改用 C++ 编译器而不是使用 C 编译器会改变什么?
  • @TedLyngmo 我真的尝试过 cpp、gcc 和 g++。我想我应该使用正常的 cmake 负担来编译它
  • cpp 不是一般意义上的编译器,而是预处理器。 gcc 应该用于 C 程序,g++ 应该用于 C++ 程序。

标签: c++ opencv visual-studio-code ubuntu-20.04


【解决方案1】:

几天后,我找到了解决方案。

这是为构建任务添加必要的标志。

我在GitHub 上发布了完整示例。希望它会在未来对某人有所帮助。

task.json 应该是

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

缺少的是添加这些参数

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-23
    • 1970-01-01
    • 2020-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-12
    相关资源
    最近更新 更多