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.
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)
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.
After opening the Terminal app, type clang -v
or clang --version
check if your Mac comes with the clang compiler. The figure below shows the display of clang installed.
If clang is not installed on the Mac, type in the terminal Xcode-select --install
and 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.
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
Click IntelliSense
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/clang
or /Library/Developer/CommandLineTools/usr/bin/clang++
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).
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.
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
configure according to the figure below.
A .vscode folder will be generated under the opened folder, and task.json will be automatically generated in it.
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.json
and 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.
Click the "Continue" button
to see the debugging results in the "Debug Console"
and then click "Continue" to see the next result: