morizotter
morizotter

Reputation: 1974

How to compile Opencv with Visual Studio Code?

I installed opencv3 with brew. I tried many times to set pkg-config but I could not succeed to compile. I searched the way on internet but I could not find out.

The task is below:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "0.1.0",
    "command": "g++",
    "isShellCommand": true,
    "showOutput": "always",
    "suppressTaskName": true,
    "tasks": [
        {
            "taskName": "build",
            "isBuildCommand": true,
            "args": [
                `pkg-config --libs opencv --cflags opencv`,
                "-o", 
                "vsc",
                "${workspaceRoot}/main.cpp",
                "-g" // Debug
            ],
            "showOutput": "always"
        }
    ]
}

My pkg-config are like below:

pkg-config --libs

$ pkg-config --libs opencv
-L/usr/local/Cellar/opencv3/3.1.0_3/lib -lopencv_shape -lopencv_stitching -lopencv_objdetect -lopencv_superres -lopencv_videostab -lippicv -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_ml -lopencv_imgproc -lopencv_flann -lopencv_core

pkg-config --cflags

$ pkg-config --cflags opencv
-I/usr/local/Cellar/opencv3/3.1.0_3/include/opencv -I/usr/local/Cellar/opencv3/3.1.0_3/include

Do you know how to compile opencv with Visual Studio Code??

Upvotes: 4

Views: 7596

Answers (1)

CognitiveRobot
CognitiveRobot

Reputation: 1505

OS: Ubuntu 16.04, OpenCV version: 3.2.0, program filename: videocapture_starter.cpp, and output filename: videocapture_starter

To build I used the following task.json file

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "0.1.0",
    "command": "g++",
    "isShellCommand": true,
    "showOutput": "always",
    "args": ["-g", "videocapture_starter.cpp", "-o", "videocapture_starter", "-I/usr/local/include/opencv", "-I/usr/local/include","-I/usr/include", "-L/usr/local/lib",
     "-lopencv_shape", "-lopencv_stitching", "-lopencv_objdetect", "-lopencv_superres", "-lopencv_videostab", "-lopencv_calib3d",
     "-lopencv_features2d", "-lopencv_highgui", "-lopencv_videoio", "-lopencv_imgcodecs", "-lopencv_video", "-lopencv_photo", 
     "-lopencv_ml", "-lopencv_imgproc", "-lopencv_flann", "-lopencv_core"]
}

To debug the program I used the following launch.json file

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "C++ Launch",
      "type": "cppdbg",
      "request": "launch",
      "program": "${workspaceRoot}/videocapture_starter",
      "args": ["0"],
      "stopAtEntry": false,
      "cwd": "${workspaceRoot}",
      "environment": [],
      "externalConsole": true,
      "linux": {
        "MIMode": "gdb",
        "includePath": ["/usr/include", "/usr/local/include/opencv", "/usr/local/include"],
        "setupCommands": [
          {
            "description": "Enable pretty-printing for gdb",
            "text": "-enable-pretty-printing",
            "ignoreFailures": true
          }]
      }
    },
    {
      "name": "C++ Attach",
      "type": "cppdbg",
      "request": "attach",
      "program": "${workspaceRoot}/a.out",
      "processId": "${command:pickProcess}",
      "linux": 
        {
        "MIMode": "gdb",
        "setupCommands": [
          {
            "description": "Enable pretty-printing for gdb",
            "text": "-enable-pretty-printing",
            "ignoreFailures": true
          }]
        }
    }
  ]
}

Upvotes: 3

Related Questions