VsCode 配置C++调试运行

  • 打开命令面板快捷键为F1,软件上写的Ctrl+Shift+P似乎没用

先安装插件使得可以运行

  • 先自行在vsc扩展中搜索C++安装C/C++插件
  • 再参考知乎专栏中安装code-runner插件使得可以运行,参考这位知乎博客的插件参数配置,代码使用同样是这个人的知乎回答,他的知乎回答中的代码加上了必备的参数(注意.c和.cpp文件无法使用同一个配置)

配置断点调试环境

  • 参考这位CSDN博主配置存放代码文件夹下的.vscode中的launch.jsontasks.json

  • 需要注意的是,在VSC中调试需要在打开的文件夹中进行(注意!是用VSC打开文件夹!!,单独打开一个单独的cpp是没有下面的几个配置文件出现的!!也就是说,这个cpp要在一个文件夹里!不像DevCpp和C-Free可以直接调试一个单独的cpp!!)

  • VSC不支持路径中含中文的文件调试

  • 条件断点的使用

    # VsCode 配置C++调试运行-LMLPHP

  • 特别注意:

    # VsCode 配置C++调试运行-LMLPHP

  • Vsc打开其他文件时,中文显示乱码,一劳永逸解决方案:打开Vsc,文件-首选项-设置-搜素files.autoGuessEncoding": flase改为files.autoGuessEncoding": true

  • Vsc终端打印出现中文乱码,首先我们应该明白,Vsc的终端调用的是windows的cmd或者windows powershell,到底是哪个可以通过# VsCode 配置C++调试运行-LMLPHP

    先新建一个终端,保证有两个终端以上的时候就可以看到选择默认的shell这个选项,里面可以选择是调用cmd还是powershell,通过chcp命令可以查到这两个终端的默认编码都是936(简体中文默认的GBK编码),可以通过命令chcp 65001(65001 UTF-8代码页)临时将编码改成UTF-8。不建议直接修改cmd和powershell的默认编码,这会导致国内有些使用GBK编码的软件出现乱码或者不能显示或者莫名其妙的Bug,比如有道词典。详情查看知乎马甲回答下的评论,等一些老软件全部更新成现代Unicoded软件的时候就可以更改windows的编码了,具体方法网上很多,这里不再累赘。

  • 最后配上一份我本地的配置代码

launch.json:
{
"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": "C:/MinGW/bin/gdb.exe", // miDebugger的路径,注意这里要与MinGw的路径对应
"preLaunchTask": "g++", // 调试会话开始前执行的任务,一般为编译程序,c++为g++, c为gcc
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
} tasks.json
{
// {
// // 有关 tasks.json 格式的文档,请参见
// // https://go.microsoft.com/fwlink/?LinkId=733558
// "version": "2.0.0",
// "tasks": [
// {
// "type": "shell",
// "label": "g++",
// "command": "C:\\MinGW\\bin\\g++.exe",
// "args": [
// "-g",
// "${file}",
// "-o",
// "${fileDirname}\\${fileBasenameNoExtension}.exe"
// ],
// "options": {
// "cwd": "C:\\MinGW\\bin"
// },
// "problemMatcher": [
// "$gcc"
// ],
// "group": {
// "kind": "build",
// "isDefault": true
// }
// }
// ],
// } {
"version": "2.0.0",
"command": "g++",
"args": ["-g","${file}","-o","${fileBasenameNoExtension}.exe"], // 编译命令参数
"problemMatcher": {
"owner": "cpp",
"fileLocation": ["relative", "${workspaceFolder}"],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
}
}
  • 本地用户设置代码
settings.json
{
"files.autoSaveDelay": 500,
"editor.fontSize": 18,
"code-runner.clearPreviousOutput": true,
"code-runner.ignoreSelection": true,
"code-runner.preserveFocus": false,
"code-runner.runInTerminal": true,
"code-runner.respectShebang": false,
"code-runner.saveAllFilesBeforeRun": true,
"code-runner.saveFileBeforeRun": true,
"code-runner.executorMap": {
"c": "cd $dirWithoutTrailingSlash && gcc $fileName -o $fileNameWithoutExt.exe -std=c11 -Wall -lm && ./$fileNameWithoutExt.exe",
//"c": "cd $dir && gcc -std=c11 $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
//"cpp": "cd $dir && g++ -std=c++1s4 $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
"cpp": "cd $dirWithoutTrailingSlash && g++ $fileName -o $fileNameWithoutExt.exe -std=c++11 -Wall -lm && ./$fileNameWithoutExt.exe",
//"c": "cd $dir && gcc -std=c11 $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
},
"files.autoSave": "afterDelay",
"files.autoGuessEncoding": true,
"terminal.integrated.shell.windows": "C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",
}
05-11 20:30