MacOs uses VS Code to compile and debug C language programs

Reference blog: Windows/macOS uses VSCode to build a C/C++ development/Debug environment

1. Install VS Code on macOS

Download VS Code for macOS from Microsoft official website.insert image description here

2. Search and install the following extensions in the extension of VS Code

  • C/C++
  • C/C++ Extension Pack
    (The extension pack contains C/C++, C/C++ Themes, CMake Tools and CMake, which are also installed together)
  • Chinese(Simplified)
  • Code Runner (run code)
    After installing Code Runner, remember to check "Run In Terminal" in its extended settings
  • CodeLLDB (Debug debugging environment)
    Extensions to install

insert image description here

insert image description here
insert image description here
insert image description here

3. Install the compiler

The clang compiler is used here, first open the command line/terminal (terminal): search for terminal on the desktop, and open the "terminal" application.
insert image description here

After opening the Terminal app, type clang -vor clang --versioncheck if your Mac comes with the clang compiler. The figure below shows the display of clang installed. insert image description here
If clang is not installed on the Mac, type in the terminal Xcode-select --installand press Enter, and click OK in the pop-up window.
Suggestion: After the clang installation is complete, run it in the terminal clang -v -E -x c++ -and copy the selected path, which will be useful for configuring the include path in the IntelliSense configuration later.
insert image description here

4. Configure VS Code to use the compiler in the macOS environment

Create a new folder on the desktop (don't name it in Chinese, remember!), and open it in VScode.

Create a new .c file in the folder opened by VScode, and then follow the steps shown in the figure.

Click on Extended C/C++ to make extended settings
insert image description here
Click IntelliSenseinsert image description here
to set "Compiler Path" or "Complier Path" (only Chinese and English are different, there is no difference). Set it to /Library/Developer/CommandLineTools/usr/bin/clangor /Library/Developer/CommandLineTools/usr/bin/clang++insert image description here
and then set "IntelliSense Mode" or "IntelliSense Mode", select macos-clang-arm64(depending on the model and chip, if it is Apple silicon, select arm/arm64).
insert image description here

Suggestion: Include Path Settings for VSCode IntelIiSence

Continue to set the include path/Include Path to clang -v -E -x c++ -include the several paths just run in the terminal.
insert image description here

5. Configure the Debug environment

Write a simple code in the C language file just now or use the following code directly:

#include<stdio.h> 
int main() 
{
    for (int i = 0; i < 4; i++)
    {
        printf("Hello,World!\n");
    }
    return 0;
}

Make a breakpoint before the line of a certain function, click the option button next to Run, and select to debug C/C++ files to
insert image description here
insert image description here
configure according to the figure below.
insert image description here
A .vscode folder will be generated under the opened folder, and task.json will be automatically generated in it.
insert image description here
The content of task.json is as follows:

{
    "tasks": [
      {
        "type": "cppbuild",
        "label": "C/C++: clang 生成活动文件",
        "command": "/usr/bin/clang",
        "args": [
          "-fcolor-diagnostics",
          "-fansi-escape-codes",
          "-g",
          "${file}",
          "-o",
          "${fileDirname}/${fileBasenameNoExtension}"
        ],
        "options": {
          "cwd": "${fileDirname}"
        },
        "problemMatcher": [
          "$gcc"
        ],
        "group": {
          "kind": "build",
          "isDefault": true
        },
        "detail": "调试器生成的任务。"
      }
    ],
    "version": "2.0.0"
  }

Then create a new one under the .vscode folder launch.jsonand write the following code:

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
      {
        "type": "lldb",
        "request": "launch",
        "name": "Debug",
        "program": "${workspaceFolder}/${fileBasenameNoExtension}",
        "args": [],
        "cwd": "${workspaceFolder}"
      }
    ]
  }

6. Hello World!

For the code written earlier, add breakpoints to debug.
insert image description here
insert image description here
Click the "Continue" button
insert image description here
to see the debugging results in the "Debug Console"
insert image description here
and then click "Continue" to see the next result:
insert image description here

The above is the whole content of MacOs using VS Code to compile and debug C language programs~~

Guess you like

Origin blog.csdn.net/han_xj/article/details/129708116