【问题标题】:Visual Studio Code - configure OpenCV libraries for C++Visual Studio Code - 为 C++ 配置 OpenCV 库
【发布时间】:2021-06-16 22:41:26
【问题描述】:

我已经在 Ubuntu 上成功安装了 OpenCV,并且我成功地运行了一个示例代码:

g++ main.cpp -o testoutput -std=c++11 `pkg-config --cflags --libs opencv` 

我尝试使用 Visual Studio Code 运行它,我已经安装了 C/C++ 和代码运行器的扩展并使用以下配置运行它:

tasks.json:

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++ build active file",
            "command": "/usr/bin/g++",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}",
                "-std=c++11","`pkg-config","--cflags","--libs opencv`"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

launch.json:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "g++ - Build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": ["-std=c++11","`pkg-config","--cflags","--libs opencv`"],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: g++ build active file",
            "miDebuggerPath": "/usr/bin/gdb"
        }
    ]
}

我收到以下错误:

[Running] cd "/home/kfir/code/opencv_test/" && g++ main.cpp -o main && "/home/kfir/code/opencv_test/"main
main.cpp:1:10: fatal error: opencv2/core.hpp: No such file or directory
 #include <opencv2/core.hpp>
          ^~~~~~~~~~~~~~~~~~
compilation terminated.

[Done] exited with code=1 in 0.032 seconds

注意:我在 mac 上使用 VSCode,通过 ssh 连接 Ubuntu 远程机器,终端使用上面的 g++ 命令可以正常工作

【问题讨论】:

    标签: c++ opencv visual-studio-code vscode-debugger vscode-remote


    【解决方案1】:

    请务必将位置添加到您的 includePath 中的 openCV 头文件。

    c_cpp_properties.json 文件:

    {
        "configurations": [
            {
                "name": "Linux",
                "includePath": [
                    "${default}",
                    "~/opencv4.5-custom/include/opencv4"
                ],
                "defines": [],
                "compilerPath": "/usr/bin/gcc",
                "cStandard": "gnu17",
                "cppStandard": "gnu++14",
                "intelliSenseMode": "gcc-x64"
            }
        ],
        "version": 4
    }
    

    我说的是“~/opencv4.5-custom/include/opencv4”,但它可能是“/usr/include/opencv4”或其他内容,具体取决于您安装 openCV 的方式和位置。

    如果要在终端中运行,您还需要修改 task.json 以将参数添加到编译器,如 pkg-config --cflags --libs opencv4 命令给出的那样。您需要提供共享对象文件的路径。

    这是我的 task.json 文件的内容:

    {
        "tasks": [
            {
                "type": "cppbuild",
                "label": "C/C++: g++ build active file",
                "command": "/usr/bin/g++",
                "args": [
                    "-g",
                    "${file}",
                    "-o",
                    "${fileDirname}/${fileBasenameNoExtension}",
                    "-I", "~/opencv4.5-custom/include/opencv4",
                    "-L", "~/opencv4.5-custom/lib",
                    "-l", "opencv_core",
                    "-l", "opencv_videoio",
                    "-l", "opencv_imgproc",
                    "-l", "opencv_highgui"
                ],
                "options": {
                    "cwd": "${workspaceFolder}"
                },
                "problemMatcher": [
                    "$gcc"
                ],
                "group": {
                    "kind": "build",
                    "isDefault": true
                },
                "detail": "Task generated by Debugger."
            }
        ],
        "version": "2.0.0"
    }
    

    您还需要根据您的设置更改路径。

    这里我只包含了我的程序使用的模块("-l", "opencv_core" 等行......)所以根据您的需要添加或删除模块。

    【讨论】:

      猜你喜欢
      • 2022-11-26
      • 2017-02-10
      • 2023-01-17
      • 2011-07-04
      • 2023-03-26
      • 1970-01-01
      • 2019-11-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多