软件安装
- 安装vscode
- 安装MinGW
选择mingw32-base以及mingw32-gcc-g++
配置mingw环境变量F:\MinGW\bin
cmd中检查mingw是否安装成功:输入gcc -v - vscode安装插件
C/C++、C++ Intellisense、Code Runner 和Include Autocomplete
配置环境文件
- 新建项目文件夹以及代码文件
- 在项目文件夹的.vscode文件夹下建立三个配置文件分别是:c_cpp_properties.json、tasks.json、launch.json
例如我的项目路径是:E:\workspace\VSCode\C++\hello world,则所建立的三个配置文件的路径在E:\workspace\VSCode\C++\hello world\.vscode\文件夹下,如果不自己添加,在直接运行cpp文件的时候试错,vscode会让你一步一步添加的。
三个配置文件的内容如下:
//c_cpp_properties.json
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "D:\\MinGW\\bin\\gcc.exe",
"intelliSenseMode": "clang-x64",
"browse": {
"path": [
"${workspaceFolder}"
],
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": ""
},
"cStandard": "c11",
"cppStandard": "c++17"
},
{
"name": "Linux",
"includePath": [
"/usr/include",
"/usr/local/include",
"${workspaceFolder}"
],
"defines": [],
"intelliSenseMode": "clang-x64",
"browse": {
"path": [
"/usr/include",
"/usr/local/include",
"${workspaceFolder}"
],
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": ""
}
},
{
"name": "Mac",
"includePath": [
"/usr/include",
"/usr/local/include",
"${workspaceFolder}"
],
"defines": [],
"intelliSenseMode": "clang-x64",
"browse": {
"path": [
"/usr/include",
"/usr/local/include",
"${workspaceFolder}"
],
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": ""
},
"macFrameworkPath": [
"/System/Library/Frameworks",
"/Library/Frameworks"
]
}
],
"version": 4
}
//tasks.json
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build", //在launch.json文件中会用到
"type": "shell",
"command": "g++",
"args": [
"-g", "${file}","-o","${workspaceFolder}/${fileBasenameNoExtension}.exe"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
//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) Launch", // 配置名称,将会在启动配置的下拉菜单中显示
"type": "cppdbg", // 配置类型,这里只能为cppdbg
"request": "launch", // 请求配置类型,可以为launch(启动)或attach(附加)
"targetArchitecture": "x86", // 生成目标架构,一般为x86或x64,可以为x86, arm, arm64, mips, x64, amd64, x86_64
"program": "${workspaceFolder}/${fileBasenameNoExtension}.exe", // 将要进行调试的程序的路径
"args": [], // 程序调试时传递给程序的命令行参数,一般设为空即可
"stopAtEntry": false, // 设为true时程序将暂停在程序入口处,一般设置为false
"cwd": "${workspaceFolder}", // 调试程序时的工作目录,一般为${workspaceRoot}即代码所在目录
"environment": [],
"externalConsole": true, // 调试时是否显示控制台窗口,一般设置为true显示控制台
"MIMode": "gdb",
"miDebuggerPath": "D:\\MinGW\\bin\\gdb.exe", // miDebugger的路径,注意这里要与MinGw的路径对应
"preLaunchTask": "build", // 调试会话开始前执行的任务,与tasks.json中的label的值一样
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
然后就可以顺利运行你的C++程序了