windows 10上使用vscode编译运行和调试C_C++
VScode环境搭建
- 1. MinGW安装配置
- MinGW下载安装
- 设置环境变量
- 2. Vscode安装配置
- Vscode安装
- 安装C/C++插件
- 新建C/C++工程
- 启动配置文件(launch.json)
- 添加编译任务(tasks.json)
1. MinGW安装配置
MinGW下载安装
http://www.mingw-w64.org/doku.php
设置环境变量
安装完成MinGW64后将其添加至环境变量,即把C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin添加至系统环境变量。
注意:这里的C:\MinGW\bin与你的安装路径有关!
下面在命令提示符或者Windows PowweShell中输入gcc -v测试是否配置成功,如果得到如下结果则表示成功:
2. Vscode安装配置
Vscode安装
在Vscode官网下载安装包:https://code.visualstudio.com/
安装完成后打开Vscode,主界面如下:
安装C/C++插件
点击左侧Extension按钮来选择安装插件:
搜索C++,点击Install
新建C/C++工程
由于Vscode以文件夹的形式管理工程,所以需要新建一个文件夹来管理工程,这里建立一个名为hello的文件夹。
然后用Vscode打开该文件夹:
打开后得到一个空的工程:
快捷键Ctrl+N新建一个C文件,取名为helloworld.c,并输入一个简单的程序:
启动配置文件(launch.json)
点击左边栏的Debug按钮,然后选择配置launch.json文件,操作如下:
选择C++(GDB\LLDB),自动打开launch.json文件:
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",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\gdb.exe",
"preLaunchTask": "build",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
注意
"miDebuggerPath":
"C:/MinGW/bin/gdb.exe"与你安装的路径有关!
"preLaunchTask":
"build"表示设置启动前的任务为"build",下面通过tasks.json来添加"build"任务。
添加编译任务(tasks.json)
利用快捷键ctrl+shift+p打开命令行,输入Tasks: Run task,然后依次选择:
> No task to run found. configure
tasks...
> Create tasks.json file from template
> Others Example to run an arbitrary external
command.
打开tasks.json文件编辑内容如下:
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "shared"
},
"windows": {
"command": "g++",
"args": [
"-ggdb",
"\"${file}\"",
"--std=c++11",
"-o",
"\"${fileDirname}\\${fileBasenameNoExtension}.exe\""
]
}
}
]
}