操做是在windows环境,linux应该操做差很少
我这里用到的是minGWlinux
安装完成后还须要添加make,执行下面命令,由于我须要用到Makefile文件shell
mingw-get install mingw32-make
先来试试调试,按下F5的时候若是你没有launch.js文件,则会提示你建立一个,而后选择 C++(GDB/LLDB)json
建立以后先编译一个程序试试windows
#include <stdio.h> int main(){ printf("Hello,World!\n"); char a=getchar(); printf("input char:%c\n",a); return 0; } //编译加 -g 参数 //gcc -g heelo.c -o hello //在同一目录下生成 hello.exe
配置launch.jsonui
{ // 使用 IntelliSense 了解相关属性。 // 悬停以查看现有属性的描述。 // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "name": "(gdb) Launch",//调试的名字 "type": "cppdbg", "request": "launch", "program": "hello.exe",//程序的目录 "args": [],//参数 无论 "stopAtEntry": false, "cwd": "${workspaceFolder}",//运行目录 "environment": [], "externalConsole": true, "MIMode": "gdb", "miDebuggerPath": "E:\\mingw\\bin\\gdb.exe",//gdb目录 "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ] } ] }
而后再在哪一个文件按下F5,就能够愉快的下断点调试了,左侧能够查看变量的值spa
而后由于我有一些其余的想法,用到了Makefile,我得先make一次,而后再编译,若是按照上面的方法就得先在cmd里面输入一次make,而后再f5调试,我想直接f5,一步完成,最主要的是在launch.json里面增长一项配置preLaunchTask,配置先执行的任务
先写一下Makefile3d
g=gcc -g obj=hello.o hello:$(obj) $(g) $(obj) -o hello hello.o: $(g) -c hello.c
make一次,还不错,而后改一下launch.json文件调试
{ // 使用 IntelliSense 了解相关属性。 // 悬停以查看现有属性的描述。 // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "name": "(gdb) Launch", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}\\hello.exe", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": true, "MIMode": "gdb", "miDebuggerPath": "E:\\mingw\\bin\\gdb.exe", "preLaunchTask": "build",//先运行的任务build是任务名字 "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ] } ] }
而后会提示你建立一个task.json的文件code
{ // See https://go.microsoft.com/fwlink/?LinkId=733558 // for the documentation about the tasks.json format "version": "2.0.0", "tasks": [ { "label": "build", "type": "shell", "command": "mingw32-make",//执行的命令,make文件路径 我这里配置了环境变量,因此能够直接写 "args": ["hello"]//执行的时候后面附加的参数 } ] }
而后F5的时候,就能够直接调试啦,下面终端是显示的执行的命令orm
我这里没写args,写了后终端应该显示 > Executing task: mingw32-make hello <
而后记录一下VSCode的一些自带变量
官方文档:https://code.visualstudio.com/docs/editor/variables-reference
${workspaceRoot} 当前打开的文件夹的绝对路径+文件夹的名字
${workspaceRootFolderName} 当前打开的文件夹的名字
${file} 当前打开正在编辑的文件名,包括绝对路径,文件名,文件后缀名
${relativeFile} 从当前打开的文件夹到当前打开的文件的路径
${fileBasename} 当前打开的文件名+后缀名,不包括路径
${fileBasenameNoExtension} 当前打开的文件的文件名,不包括路径和后缀名
${fileDirname} 当前打开的文件所在的绝对路径,不包括文件名
${fileExtname} 当前打开的文件的后缀名
${cwd} 启动时的当前工做目录
${lineNumber} 当前打开的文件,光标所在的行数