kalonymus
kalonymus

Reputation: 31

Visual Studio Code - configure OpenCV libraries for C++

I've installed OpenCV On Ubuntu successfully and I managed to run a sample code as:

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

I've tried to run it with Visual Studio Code, I've installed the extension of C/C++ and code runner and ran it with the following configuration:

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

I got the following error:

[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

Note: I'm using VSCode on mac and connect Ubuntu remote machine by ssh, the terminal works fine with the command of g++ above

Upvotes: 2

Views: 2404

Answers (1)

GwelDG
GwelDG

Reputation: 11

Be sure to add the location to openCV header files in your includePath.

c_cpp_properties.json file :

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

Mine says "~/opencv4.5-custom/include/opencv4" but it could be "/usr/include/opencv4" or something else, depending on how and where you installed openCV.

You also need to modify task.json to add the arguments to the compiler as given by the pkg-config --cflags --libs opencv4 command, if you would run it in the terminal. You'll need to give the path to the shared object files.

Here's the content of my task.json file :

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

You'll also need the change the paths depending to you setup.

Here I have only included the modules that are used by my program (with the lines "-l", "opencv_core" and so on...) so add or remove modules according to your needs.

Upvotes: 1

Related Questions