Visual Studio Code安装与设置

Windows

安装编译器

  1. WinLib下载GCC 14.2.0 (with POSIX threads) + LLVM/Clang/LLD/LLDB 19.1.7 + MinGW-w64 12.0.0 UCRT - release 3 (LATEST)
  2. 解压缩文件,将整个文件夹(mingw64)复制到C:
  3. 设置环境变量PATH,增加:C:
  4. 执行以下命令,显示软件版本号为止。
1
g++ --version

安装Visual Studio Code

  1. 浏览Visual Studio Code下载地址根据操作系统,下载相应的版本:

  2. Visual Studio Code for Windows 1.97.2

  3. Visual Studio Code for macOS

  4. 安装Visual Code for Windows

  5. 安装中文语言插件(Ctrl+Shift+X):输入Chinese,点击Install按钮。安装完成后,点击Change Language and Restart。重启后进入中文界面。

  6. 设置浅色界面:设置,主题,颜色主题(或按下Ctrl+K,Ctrl+T快捷键),选择浅色Visual Studio。

  7. 安装C++插件(Ctrl+Shift+X):输入C++,找到Microsoft的C++插件,点击安装

  8. 安装C++扩展插件,如上,找到Microsoft的C++ Extensions插件,点击安装。 ### 设置编译环境

  9. 设置C++编译环境,自动进入到欢迎界面,勾选Setup Your C++ Environment,Select My Default Compiler,选择使用G++.exe,在C:。

  10. 也可以再设置编译和调试的配置文件,如下:

创建C++源代码文件

  1. 在桌面上创建cpp文件夹作为工作区,C++源代码文件保存在此工作区。
  2. 新建hello.cpp文件,输入源代码
1
2
3
4
5
6
7
8
#include<iostream>
using namespace std;

int main()
{
char *str = "hello,world.";
cout << str;
}

配置编译任务

  1. Ctrl+Shift+P 打开命令面板,输入 Tasks: Configure Task,选择 Create tasks.json file from templateOthers
  2. 修改生成的 tasks.json 文件,配置编译任务:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe 生成活动文件",
"command": "c:\\mingw64\\bin\\g++.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "c:\\mingw64\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "调试器生成的任务。"
}
],
"version": "2.0.0"
}

配置调试(可选)

  1. 点击左侧调试图标(或按 Ctrl+Shift+D),点击 create a launch.json file,选择 C++ (GDB/LLDB)
  2. 修改 launch.json 文件,确保路径正确:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) 启动",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "gdb.exe",
"preLaunchTask": "cppbuild", // 关联编译任务
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "将反汇编风格设置为 Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
}

]
}

编译

  • Ctrl+Shift+B 执行编译任务,生成 .exe 文件。

运行

  • 直接按 F5 启动调试(需配置 launch.json),或手动在终端运行生成的 .exe 文件。

macOS

安装 Xcode Command Line Tools

1
xcode-select --install # 弹出窗口后点击安装

安装 Visual Studio Code

  • 官网 下载并安装

安装 C++ 扩展插件

  1. 打开 VSCode
  2. Cmd+Shift+X 打开扩展市场
  3. 搜索并安装 C++ 扩展(由 Microsoft 提供)

创建项目文件夹

1
2
mkdir cpp
cd cpp

编写测试代码

新建文件 hello.cpp

1
2
3
4
5
6
7
#include <iostream>
using namespace std;

int main() {
cout << "Hello VSCode C++ on macOS!" << endl;
return 0;
}

配置编译任务 (tasks.json)

  1. Cmd+Shift+P 打开命令面板
  2. 输入 Configure Tasks,选择 Create tasks.json file from template
  3. 选择 Others
  4. 替换内容为以下配置:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
{
"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"
}

配置调试 (launch.json)

  1. 点击左侧 Run and Debug 图标(或按 Cmd+Shift+D
  2. 点击 create a launch.json file
  3. 选择 C++ (GDB/LLDB)
  4. 替换内容为以下配置:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{
"configurations": [
{
"name": "(lldb) 启动",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": true,
"MIMode": "lldb",
"preLaunchTask": "C/C++: clang 生成活动文件",
}
]
}

编译和运行

编译

  • Cmd+Shift+B 编译代码(自动生成可执行文件)

运行

  • F5 启动调试(或点击 Run > Start Debugging
  • 在终端中查看输出结果

常见问题

找不到头文件?

  • 确保已安装 Xcode Command Line Tools
  • 检查编译器路径:终端输入 which clang++ 应返回 /usr/bin/clang++

编译参数说明

  • -std=c++17: 指定 C++ 标准版本
  • -stdlib=libc++: macOS 默认 C++ 标准库
  • -g: 生成调试信息

直接使用终端编译

1
clang++ -std=c++17 -stdlib=libc++ hello.cpp -o hello && ./hello

通过以上步骤,你可以在 macOS 的 VSCode 中高效编译和调试 C++ 代码。