一:准备工作:
1.下载并安装Vscode
下载链接:https://code.visualstudio.com/Download
2.计算机配置
WIN10 64位
3.下载MinGW
下载链接:https://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win32/Personal%20Builds/mingw-builds/installer/mingw-w64-install.exe/download
安装要求:不要有中文,不要有空格
安装MinGW注意事项
在线安装MinGW-W64提示cannot download repository.txt,如图以下:
把网络切换成手机热点,再进行安装就可以
安装过程配置选项
配置如图所示:
配置系统环境变量Path
将MinGW-W64安装目录下面的bin文件夹路径添加至path环境变量
添加完成后,打开命令终端,输入以下命令,若出现如图片所示的回应,则表示安装成功。
4.安装C/C++解释器
点击安装即可
二:开始配置
1 创建工程文件夹
创建两个文件夹,一个文件夹命名Project,一个文件夹命名Hello,其中Hello为子文件夹。
2 在Vscode中打开文件夹
新建文件,并且命名为以.cpp结尾的文件名。
在文件中输入以下代码
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<string> msg {"Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"};
for (const string& word : msg)
{
cout << word << " ";
}
cout << endl;
}
勾选自动保存按钮,可以自动保存文件
三:编译文件
创建一个 tasks.json文件
点击Terminal,选中Configure Default Build Task,选中g++.exe build active file
tasks.json文件内容如下所示:
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "g++.exe build active file",
"command": "D:\\Program Files\\mingw-w64\\mingw64\\bin\\g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "D:\\Program Files\\mingw-w64\\mingw64\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
创建一个launch.json文件
点击 Run ,点击 Add Configuration然后选择C++ (GDB/LLDB).
launch.json文件内容如下所示:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: 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": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "D:\\Program Files\\mingw-w64\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "g++.exe build active file"
}
]
}
修改c_cpp_properties.json文件
打开命令面板,输入C/C++,选中 C/C++: Edit Configurations (UI) ,打开c_cpp_properties.json文件,将里面的代码修改为
{
"configurations": [
{
"name": "GCC",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.18362.0",
"compilerPath": "D:/Program Files/mingw-w64/mingw64/bin/g++.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}
配置C语言类似,只需要把g++改成gcc即可
launch.json文件如下
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: 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": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "D:\\Program Files\\mingw-w64\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "gcc.exe build active file"
}
]
}
tasks.json文件如下
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "gcc.exe build active file",
"command": "D:\\Program Files\\mingw-w64\\mingw64\\bin\\gcc.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
c_cpp_properties.json文件如下
{
"configurations": [
{
"name": "GCC",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.18362.0",
"compilerPath": "D:\\Program Files\\mingw-w64\\mingw64\\bin\\gcc.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}
四:出现的问题
在配置好之后,编译时,提示没有.exe文件。如下图所示
找了好多方法都不顶用,最后将腾讯电脑管家关闭就好了。原因可能是腾讯电脑管家将生成的.exe可执行文件删除了。