1、所需软件
1、vscode,目前最省心的开发工具,下载地址: https://code.visualstudio.com/Download
2、MinGW,Windows特定头文件和使用GNU工具集导入库的集合,其实单独的MinGW就是一个c++编译环境,vscode只是给它套了个壳,提供了一个可操作的界面。我自用的MinGW下载地址:下载x86_64-win32-sjlj,官网那个下半天没下下来,应该是需要翻墙。把下载下来的文件夹解压,放到C盘根目录下,再在系统变量里面配置一下系统路径,一般是:C:\mingw64\bin
配置完后,在控制台里面输入 g++ -v,查看是否配置成功,出现如下样式即是成功。
2、在vscode中安装必要c++插件
在vscode左侧找到插件库,安装 c/c++ 插件(必须)、C++ Intellisense(非必须)、Include Autocomplete(非必须)。
3、修改vscode配置文件
新键一个文件夹作为主目录,用vscode打开,再在文件夹内新建一个.vscode作为配置目录,目录下再新建3个配置文件,分别为c_cpp_properties.json、launch.json、tasks.json。
c_cpp_properties.json,其中要注意mingw64的地址要和自己的对应
{
"configurations": [
{
"name": "Win64",
"includePath": [
"${workspaceRoot}",
"C:/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
"C:/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
"C:/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",
"C:/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include",
"C:/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/tr1",
"C:/mingw64/x86_64-w64-mingw32/include"
],
"defines": [
"_DEBUG",
"UNICODE",
"__GNUC__=6",
"__cdecl=__attribute__((__cdecl__))"
],
"intelliSenseMode": "msvc-x64",
"browse": {
"path": [
"${workspaceRoot}",
"C:/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
"C:/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
"C:/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",
"C:/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include",
"C:/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/tr1",
"C:/mingw64/x86_64-w64-mingw32/include"
]
},
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": "",
"compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.22.27905/bin/Hostx64/x64/cl.exe",
"cStandard": "c11",
"cppStandard": "c++17"
}
],
"version": 4
}
launch.json,这里也要注意miDebuggerPath的地址
{
"version": "0.2.0",
"configurations": [
{
"name": "C++ Launch (GDB)", // 配置名称,将会在启动配置的下拉菜单中显示
"type": "cppdbg", // 配置类型,这里只能为cppdbg
"request": "launch", // 请求配置类型,可以为launch(启动)或attach(附加)
"targetArchitecture": "x64", // 生成目标架构,一般为x86或x64,可以为x86, arm, arm64, mips, x64, amd64, x86_64
"program": "${file}.exe", // 将要进行调试的程序的路径
"miDebuggerPath": "c:\\mingw64\\bin\\gdb.exe", // miDebugger的路径,注意这里要与MinGw的路径对应
"args": [
"blackkitty",
"1221",
"# #"
], // 程序调试时传递给程序的命令行参数,一般设为空即可
"stopAtEntry": false, // 设为true时程序将暂停在程序入口处,一般设置为false
"cwd": "${workspaceRoot}", // 调试程序时的工作目录,一般为${workspaceRoot}即代码所在目录
"externalConsole": true, // 调试时是否显示控制台窗口,一般设置为true显示控制台
"preLaunchTask": "g++" // 调试会话开始前执行的任务,一般为编译程序,c++为g++, c为gcc
}
]
}
tasks.json,这个一般不做修改
{
"version": "0.1.0",
"command": "g++",
"args": [
"-g",
"${file}",
"-o",
"${file}.exe"
], // 编译命令参数
"problemMatcher": {
"owner": "cpp",
"fileLocation": [
"relative",
"${workspaceRoot}"
],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
}
}
4、运行看看
创建一个test.cpp文件,再点击左上角的c++ launch(GBD),运行看看。
#include <iostream>
using namespace std;
int main() {
cout << "Hello,world!" << endl;
system("pause");
return 0;
}