4

I have been trying to setup Visual Studios Code for my Mac and I am having issues with the launch.json and the task.json. I will be using the Clang compiler.

I tried following microsoft's documentation, but it sets up the .JSON files to compile and debug a program named helloworld.c, I just want to configure the launch.json and the task.json to build and debug any .c/.cpp file I give it. I am not experienced enough with .JSON files to know what I am doing or do anything that works.

tasks.json:

{
"version": "2.0.0",
"tasks": [
    {
        "label": "clang++ build active file",
        "type": "shell",
        "command": "clang++",
        "options": {
            "cwd": "${workspaceRoot}"
        },
        "group": "build",
        "presentation": {
            "echo": true,
            "reveal": "always",
            "focus": false,
            "panel": "shared"
        },
        "args": [
            "-std=c++17",
            "-stdlib=libc++",
            "--debug"
        ],
        "problemMatcher": {
            "owner": "cpp",
            "fileLocation": [
                "absolute"
            ],
            "pattern": {
                "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
                "file": 1,
                "line": 2,
                "column": 3,
                "severity": 4,
                "message": 5
            }
        }
    },
    {
        "type": "shell",
        "label": "clang build active file",
        "command": "/usr/bin/clang",
        "args": [
            "-g",
            "${file}",
            "-o",
            "${fileDirname}/${fileBasenameNoExtension}"
        ],
        "options": {
            "cwd": "/usr/bin"
        },
        "problemMatcher": [
            "$gcc"
        ],
        "group": {
            "kind": "build",
            "isDefault": true
        }
    }
]

}

launch.json:

  "version": "0.2.0",
  "configurations": [
    {
      "name": "clang build and debug active file",
      "type": "cppdbg",
      "request": "launch",
      "program": "${fileDirname}/${fileBasenameNoExtension}",
      "args": [],
      "stopAtEntry": false,
      "cwd": "${workspaceFolder}",
      "environment": [],
      "externalConsole": false,
      "MIMode": "lldb",
      "setupCommands": [
        {
          "description": "Enable pretty-printing for gdb",
          "text": "-enable-pretty-printing",
          "ignoreFailures": true
        }
      ],
      "preLaunchTask": "clang build",
      "miDebuggerPath": "/usr/bin/lldb"
    }
  ]
}

3 Answers 3

4

I was struggling with this as well. I have gotten it work now. For the tasks.json you only need 1 section, and if you will have multiple cpp files in the project then you will need to add "${fileDirname}/*.cpp". Here is what my tasks.json look like:

{
  "version": "2.0.0",
  "tasks": [
    {
      "type": "shell",
      "label": "clang++ build active file",
      "command": "/usr/bin/clang++",
      "args": [
        "-std=c++17",
        "-stdlib=libc++",
        "-g",
        "${fileDirname}/*.cpp",
        // "${file}",
        "-o",
        "${fileDirname}/${fileBasenameNoExtension}"
      ],
      "options": {
        "cwd": "${workspaceFolder}"
      },
      "problemMatcher": ["$gcc"],
      "group": {
        "kind": "build",
        "isDefault": true
      }
    }
  ]
}

Also, I was having a problem with the vector ; error from the vscode example. If you are running into the same problem, try to add a c_cpp_properties.json file in .vscode directory as follows:

{
    "configurations": [
        {
            "name": "Mac",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [
            ],
            "macFrameworkPath": [
                "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
            ],
            "compilerPath": "/usr/bin/clang",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x64"
        }
    ],
    "version": 4
}
Sign up to request clarification or add additional context in comments.

Comments

1

launch:

{
  "version": "0.2.0",
  "configurations": [
    {
      // MacOS
      "name": "Launch Program(lldb)",
      "type": "cppdbg",
      "request": "launch",
      "program": "${workspaceRoot}/no1_2",
      "args": [
        "4",
        "3",
        "2",
        "1"
      ],
      "stopAtEntry": false,
      "cwd": "${workspaceRoot}",
      "environment": [],
      "externalConsole": true,
      "MIMode": "lldb"
    }

tasks:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
      {
        "type": "shell",
        "label": "Build with Clang",
        "command": "/usr/bin/clang++",
        "args": [
          "-std=c++17",
          "-stdlib=libc++",
          "${file}",
          "-o",
          "1.out",
          "-debug",
        ],
        "group": {
          "kind": "build",
          "isDefault": true
        }
      }
    ]
  }

c_cpp:

{
    "configurations": [
        {
            "name": "Mac",
            "includePath": ["${workspaceFolder}/**"],
            "defines": [],
            "macFrameworkPath": [           "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
            ],
            "compilerPath": "/usr/bin/clang",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x64"
        }
    ],
    "version": 4
}

wish to help you

Comments

0

If you just want to compile or debug only any one C/C++ file in VSCode on mac,

I highly recommend you :

  1. Setup VSCode Extensions environment;

  2. Config C/C++ compile and debug VSCode-files.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.