簡體   English   中英

Visual Studio Code - 為 C++ 配置 OpenCV 庫

[英]Visual Studio Code - configure OpenCV libraries for C++

我已經成功地在 Ubuntu 上安裝了 OpenCV,並且成功地運行了一個示例代碼:

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

我嘗試使用 Visual Studio Code 運行它,我已經安裝了 C/C++ 和代碼運行器的擴展,並使用以下配置運行它:

任務.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"
}

啟動.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++ 命令

請務必將位置添加到 includePath 中的 openCV header 文件。

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以將 arguments 添加到pkg-config --cflags --libs opencv4命令給出的編譯器中,如果您要在終端中運行它的話。 您需要提供共享 object 文件的路徑。

這是我的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"等行......)所以根據您的需要添加或刪除模塊。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM