一、事先准备
1、安装编译器(MinGW-W64 GCC)
链接: https://pan.baidu.com/s/1EhmVd97xFRtfy3V3sJzQlg
提取码: qghe
2、添加环境变量
比如我解压到这里了 D:\good\mingw64
在系统自带的搜索中搜索 “高级系统设置”
点“高级” 再点“环境变量”
找到“path”双击点进去
点“新建”
填入“D:\good\mingw64\bin” (我解压到这里了 D:\good\mingw64)
再点“确定”
就好了。
二、 C语言配置
新建一个专门写C的文件夹命名为“C”,再在“C”中新建一个文件夹命名为“.vscode” (注意 vscode前有个点)。
.vscode文件夹中新建三个文件,如下图所示。
c_cpp_properties.json
需要将有关mingw64的路径换成自己解压出来的路径。(我解压到这里了 D:\good\mingw64)
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceRoot}",
"D:/good/mingw64/include/**",
"D:/good/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
"D:/good/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
"D:/good/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",
"D:/good/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include",
"D:/good/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixed",
"D:/good/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/include"
],
"defines": [
"_DEBUG",
"UNICODE",
"__GNUC__=6",
"__cdecl=__attribute__((__cdecl__))"
],
"intelliSenseMode": "msvc-x64",
"browse": {
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": "",
"path": [
"${workspaceRoot}",
"D:/good/mingw64/include/**",
"D:/good/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
"D:/good/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
"D:/good/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",
"D:/good/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include",
"D:/good/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixed",
"D:/good/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/include"
]
}
}
],
"version": 4
}
launch.json
需要将有关mingw64的路径换成自己的路径。(我解压到这里了 D:\good\mingw64)
{
"version": "0.2.0",
"configurations": [
{
"name": "(Windows) Launch",
"type": "cppvsdbg",
"request": "launch",
"program": "cmd",
"preLaunchTask": "echo",
"args": [
"/C",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"&",
"echo.",
"&",
"pause"
],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole":true
},
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "D:\\good\\mingw64\\bin\\gdb.exe",// 自己电脑的gdb
"preLaunchTask": "echo",//这里和task.json的label相对应
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
tasks.json
直接复制粘贴就好
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "echo",
"type": "shell",
"command": "gcc",
"args": [
"-g",
"${file}",
"-o",
"${fileBasenameNoExtension}.exe",
"-fexec-charset=GBK"//解决中文乱码
]
}
],
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "shared",
"showReuseMessage": true,
"clear": false
}
}
测试hello.c
#include<stdio.h>
int main(int argc, char const *argv[])
{
printf("hello world\n");
return 0;
}
按下F5运行即可。
三、C++配置
新建一个专门写C++的文件夹命名为“C++”,再在“C++”中新建一个文件夹命名为“.vscode” (注意 vscode前有个点)。
.vscode文件夹中新建三个文件,如下图所示。
c_cpp_properties.json
需要将有关mingw64的路径换成自己的路径。(我解压到这里了 D:\good\mingw64)
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceRoot}",
// 以下7行需要修改
"D:/good/mingw64/include/**",
"D:/good/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../include",
// 将查询结果直接粘进来
"D:/good/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
"D:/good/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
"D:/good/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",
"D:/good/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include",
"D:/good/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixed"
],
"defines": [
"_DEBUG",
"UNICODE",
"__GNUC__=6",
"__cdecl=__attribute__((__cdecl__))"
],
"intelliSenseMode": "msvc-x64",
"browse": {
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": "",
"path": [
"${workspaceRoot}",
// 以下7行需要修改
"D:/good/mingw64/include/**",
"D:/good/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../include",
// 将查询结果直接粘进来
"D:/good/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
"D:/good/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
"D:/good/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",
"D:/good/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include",
"D:/good/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixed"
]
}
}
],
"version": 4
}
launch.json
需要将有关mingw64的路径换成自己的路径。(我解压到这里了 D:\good\mingw64)
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch", // 配置名称,将会在启动配置的下拉菜单中显示
"type": "cppdbg", // 配置类型,这里只能为cppdbg
"request": "launch", // 请求配置类型,可以为launch(启动)或attach(附加)
"program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",// 将要进行调试的程序的路径
"args": [], // 程序调试时传递给程序的命令行参数,一般设为空即可
"stopAtEntry": false, // 设为true时程序将暂停在程序入口处,一般设置为false
"cwd": "${workspaceFolder}", // 调试程序时的工作目录,一般为${workspaceFolder}即代码所在目录
"environment": [],
"externalConsole": true, // 调试时是否显示控制台窗口,一般设置为true显示控制台
"MIMode": "gdb",
// 这里的路径需要修改。改成自己的路径
"miDebuggerPath": "D:/good/mingw64/bin/gdb.exe",
"preLaunchTask": "g++", // 调试会话开始前执行的任务,一般为编译程序,c++为g++, c为gcc
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
tasks.json
直接复制粘贴就好。
{
"version": "2.0.0",
"command": "g++",
"args": [
"-g",
"${file}",
"-o",
"${workspaceFolder}/exe/${fileBasenameNoExtension}.exe"
], // 编译命令参数
"problemMatcher": {
"owner": "cpp",
"fileLocation": [
"relative",
"\\"
],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
},
"presentation": {
"panel": "new" //默认为“shared“表示共享,改成new之后每个进程创建新的端口
}
}
测试hello.cpp
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, world!" << "\n";
return 0;
}
按F5运行即可。