Featured image of post Configuring Vscode's C/C++ Environment for the Mac

Configuring Vscode's C/C++ Environment for the Mac

Vscode Configuration for Mac

Preparation

Install Vscode

Vscode can be downloaded by going to its official website

Go to the official website and click the button to download:

vscode official website

Once the download is complete, open the installation software to install

Install extension

After installing Vscode, you need to install the following two extensions:

  1. Code Runner
  2. CodeLLDB

Install Code Runner

Search for Code Runner within the extension.

Extended search step

Click Install to complete the installation. After successful installation, the interface is as follows:

Code Runner install sucessfully

Install CodeLLDB

Search for CodeLLDB within the extension to download and install it.

Install CodeLLDB

Configuration extensions

Code Runner Configuration

Open Extended Settings for Code Runner

Open Code Runner’s extension settings

Follow the configuration below:

Code-runner: Clear Previous Output

CODERUNNER-1

Code-runner: Ignore Selection

CodeRunner-2

Code-runner: Preserve Focus ×

CodeRunner-3

Code-runner: Run In Terminal

Code-runner: Save All Files Before Run

Code-runner: Save File Before Run

CodeRunner-4

Configuration of CodeLLDB

Open the extension configuration of CodeLLDB

CodeLLDB extension configuration

changeLldb › Launch: Terminal

Where:

integrated is the integrated terminal output using Vscode

external is output using an external terminal

CodeLLDB-1

Write the first program

First, find a location to create a new folder for your program. In Vsocde’s File->Open Folder select the folder you just created. Below, I have created a new folder TEST as an example:

The interface after opening the folder

Create a new c/cpp file, using hello.c as an example for this tutorial.

Create a new c-file

Take the following test code as an example:

1
2
3
4
5
6
#include<stdio.h>
int main()
{
    printf("Hello Word!");
    return 0;
}

Run the first program

Click on ▶️ in the upper right corner to run the program

Run the program

You can see that “Hello Word!” has been successfully output in Vscode’s terminal!

Successful output!

Debugging the first program

One click on Debugging -> Run and Debug -> C++ (GDB/LLDB)

Debugging-1

Select clang-generate and debug active files.

Debugging-2

At this point, the Launch.json file will pop up, please copy the following code into your Launch.json file:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "clang - Generate and debug active files",
            "type": "lldb",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "lldb",
            "preLaunchTask": "C/C++: clang Generate Activity File"
        }
    ]
}

Returning to the hello.c file we just had, with a new breakpoint! debug-breakpoint

Click Run->Start Debugging

Debugging-Initiate Debugging

Licensed under CC BY-NC-SA 4.0