Computer Science

Configuring the C/C++ Extension in VSCode (macOS)

Overview

After the college entrance exam ended, some classmates around me who are headed for computer science departments were getting ready to start grinding away at C/C++, and they ran into configuration problems while using VSCode on macOS. I'm writing a short article here to record the process.

The goal of this article is to configure Cpptool so that VSCode can automatically compile and debug single files.

Environment

  • Operating system: macOS 11.5.2
  • Computer: MacBook Pro (15-inch, 2018)
  • VSCode version: 1.59.0 379476f0e13988d90fab105c5c19e7abc8b1dea8
  • Cpptool version: ms-vscode.cpptools v1.5.1

Steps

Step 1 - Open Visual Studio Code, and open a folder.

Screenshot 2021-08-21 at 3.13.01 PM

Step 2 - Open the Extensions view, search for C++, and install Cpptool.

Screenshot 2021-08-21 at 3.14.12 PM

Step 3 - We now need to configure the compiler location and IntelliSense. IntelliSense is a code-writing tool that is aware of your code. Press F1 to open VSCode's Command Palette, type C/C++: Edit Configuration(UI), and run it.

Screenshot 2021-08-21 at 3.20.11 PM
Screenshot 2021-08-21 at 3.22.28 PM

Since macOS no longer supports running 32-bit apps, we configure it the macOS-clang-x64 way. Configure it as shown in the screenshot, and you can see that a .vscode subdirectory has appeared in the working folder, containing the C/C++ configuration for this working directory, c_cpp_properties.json:

{
    "configurations": [
        {
            "name": "Mac",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [],
            "macFrameworkPath": [],
            "compilerPath": "/usr/bin/clang++",
            "cStandard": "c17",
            "cppStandard": "c++17",
            "intelliSenseMode": "macos-clang-x64"
        }
    ],
    "version": 4
}

Step 4 - Now we start configuring C/C++ compilation. VSCode supports automated task execution, so we use this feature to automate our compilation.

Create a new HelloWorld program for testing. In this article we take configuring clang++ as the example.

Screenshot 2021-08-21 at 3.26.14 PM

Press F1, type Task, find Configure Task, and open it.

Screenshot 2021-08-21 at 3.26.47 PM

Select Clang++ build active file.

Screenshot 2021-08-21 at 3.27.34 PM

The .vscode/tasks.json file records how tasks are run in this directory, as shown:

Screenshot 2021-08-21 at 3.28.54 PM

Then go back to HelloWorld.cpp, press F1, find Run Build Task and open it, and select Clang++ build.

Screenshot 2021-08-21 at 3.29.28 PM
Screenshot 2021-08-21 at 3.30.48 PM

The build configuration is done.

Step 5 - Now we configure debugging. Go to VSCode's Run and Debug view:

Screenshot 2021-08-21 at 3.31.40 PM

Click “create a launch.json file” and choose “C++ (GDB/LLDB)”:

Screenshot 2021-08-21 at 3.32.04 PM

Choose Clang++, and it will then start debugging. Back in .vscode/launch.json, we can see the debug configuration that was just generated.

Screenshot 2021-08-21 at 3.35.28 PM

Here, I recommend that you set the externalConsole option to true to enable the external debug window.

That's the end of the configuration.

Comments 4

  1. Zxilly

    那大师可以教我C++吗 qaq

    1. kal0rona Author

      qwq learningman 又假假

  2. MichaelTan

    externalConsole设置为 true
    大师!!!

  3. 想拜大师为师的徒弟

    大师呀大师 你的教程过期了捏!