DEV Community

Summer Rizzo
Summer Rizzo

Posted on

My VSCode / C++ / OpenCV / MacOS Config!

Hey folks!

If any of you have started configuring C++ in VSCode, you may know that it has a potential of being a pain in the ass. Configuring the use of other libraries only adds to that complexity!

I'm a total beginner with C/C++ so it was especially a challenge for me. It took a while before I found the set of configurations that worked with my environment, so I thought I would post mine.

Note: This is not a guide - I'm assuming you have all of the necessary parts installed. This is a good guide to start with the basic config for C/C++ with VSCode on a Mac.

Here's what I'm working with:

  • MacOS Catalinia 10.15.4
  • Homebrew
  • XCode
  • Clang
  • C++ 17
  • OpenCV 4
  • VSCode with C/C++ extensions installed

I used Homebrew to install OpenCV.

Here's my c_cpp_properties.json :

{
    "configurations": [
        {
            "name": "Mac",
            "includePath": [
                "${workspaceFolder}/**",
                "/usr/local/Cellar/opencv/4.3.0/include/opencv4/**",
                "/usr/local/Cellar/opencv/4.3.0/lib"
            ],
            "defines": [],
            "macFrameworkPath":  [
                "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks"
              ],
            "compilerPath": "/usr/bin/clang",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x64"
        }
    ],
    "version": 4
}

My tasks.json :

{
    "version": "2.0.0",
  "tasks": [
    {
      "type": "shell",
      "label": "clang++ build active file",
      "command": "/usr/bin/clang++",
      "args": [
        "-std=c++17",
        "-stdlib=libc++",
        "-g",
        "${workspaceFolder}/*.cpp",
        "-o",
        "${fileDirname}/${fileBasenameNoExtension}",
        "-I/usr/local/Cellar/opencv/4.3.0/include/opencv4/**",
        "-I/usr/local/Cellar/opencv/4.3.0/include/opencv4",
        "-L/usr/local/Cellar/opencv/4.3.0",
        "-lopencv_highgui",
        "-lopencv_core", 
        "-lopencv_imgcodecs", 
        "-lopencv_imgproc", 
        "-lopencv_photo"
      ],
      "options": {
        "cwd": "${workspaceFolder}"
      },
      "problemMatcher": ["$gcc"],
      "group": {
        "kind": "build",
        "isDefault": true
      }
    }
    ]
}

As a note, everything after "-L/usr/local/Cellar/opencv/4.3.0", in args is specific to which OpenCV libraries I'm including.

And my 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": "(lldb) Launch",
      "type": "cppdbg",
      "request": "launch",
      "program": "${workspaceFolder}/loopi.out",
      "args": [],
      "stopAtEntry": false,
      "cwd": "${workspaceFolder}",
      "environment": [],
      "externalConsole": false,
      "MIMode": "lldb"
    }
  ]
}

loopi is the name of my program.

I run the build task with Terminal > Run Build Task and in my terminal I run ./loopi with any other command line args needed (for instance, in my program takes a path to an image file - this is specific to your program and not the build task).

There ya go! Hope it saves someone some headaches and time.

I'm a beginner, so if anyone sees something uneccessary or awry, please do let me know!

With <3, happy coding!

Discussion (1)

Collapse
joymaker profile image
Joymaker

This also worked for me using MacPorts instead of Homebrew, if I change
/usr/local/Cellar/opencv/4.3.0
to
/opt/local/include/opencv4
universally.

(I won't use homebrew because it makes a total mess of the ownership and permissions of the directories it installs under macOS.)