文章目录
1. 安装vscode
1.1. 应用商店安装
我的是联想的,直接进入联想应用商店,搜索vscode安装即可
1.2. 官网安装
- 进入vscode官网安装合适的版本:https://code.visualstudio.com/
- 下载完成后,双击下载下来的exe程序,一路同意过去即可,最好全部都勾选上
2. 安装编译器MinGW-w64
MinGW-w64下载路径:https://nuwen.net/files/mingw/mingw-18.0-without-git.exe
下载完成后双击exe程序,傻瓜式安装
3. 设置系统环境变量
- vscode在安装的时候需要勾选“添加到PATH(重启后生效)”,不需要额外设置系统环境变量
- MinGW设置:右键“此电脑”——“属性”——“高级系统设置”——“环境变量”——找到“用户变量”中的“Path”条目——“编辑”——“新建”——将MinGW-w64下载路径复制到新条目中(一定要写到bin文件夹)——完成
- 检验:Win+R,弹出的运行中输入cmd,在cmd界面输入以下命令,出现对应版本号则设置成功
4. 配置vscode里的环境
4.1. 安装插件
在Extension,即黄色框圈出来处,下载两个必要的插件C/C++, Code Runner
4.2. .vscode中.json文件的配置
文件目录结构如图:
- 按下快捷键shift+ctrl+p,展示全局命令面板,搜索找到C/C++ Edit Configurations(JSON),会在.vscode下自动生成一个c_cpp_properties.json文件
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "D:\\Users\\lenovo\\AppData\\Local\\Programs\\MinGW\\bin\\g++.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}
- tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "g++.exe build active file",
"command": "D:\\Users\\lenovo\\AppData\\local\\Programs\\MinGW\\bin\\g++.exe",
"args": [
"-g",
"${file}",
"${workspaceFolder}/src/*.cpp",
"-o",
"${fileDirname}\\${file}.exe",
],
"options": {
"cwd": "D:\\Users\\lenovo\\AppData\\local\\Programs\\MinGW\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": "build"
},
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "D:\\Users\\lenovo\\AppData\\local\\Programs\\MinGW\\bin\\g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "D:\\Users\\lenovo\\AppData\\local\\Programs\\MinGW\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
]
}
- launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "(Windows) Launch",
"type": "cppvsdbg",
"request": "launch",
"program": "cmd",
"args": [
"/C",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"&",
"pause"
],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true
},
{
"name": "g++.exe",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "D:/Users/lenovo/AppData/Local/Programs/MinGW/bin/gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "g++.exe build active file"
}
]
}
5. 运行测试
5.1. 编写cpp程序
#include <iostream>
using namespace std;
int main()
{
cout << "hello world" << endl;
}
5.2. 编译生成可执行文件
- 在terminal中输入命令:
g++ -g .\scr\Main.cpp -o .\scr\Main.exe
,可以看到对应目录下生成的可执行文件 - 在vscode右上角选择Run C/C++ File或Debug C/C++ File,会生成并执行exe文件