原来调试C++用的是Codeblocks,现在想用VSCode试一下,结果还真的成功了。
首先,CodeBlocks编译的时候会输出命令,把这块的命令记下来,后面有用:
g++.exe -Wall -fexceptions -finput-charset=GBK -g -finput-charset=UTF-8 -c C:\Projects\GLSL\main.cpp -o obj\Debug\main.o
g++.exe -Wall -fexceptions -finput-charset=GBK -g -finput-charset=UTF-8 -c C:\Projects\GLSL\Sphere.cpp -o obj\Debug\Sphere.o
windres.exe C:\Projects\GLSL\main.rc obj\Debug\res.o
g++.exe -o bin\Debug\GLSL.exe obj\Debug\main.o obj\Debug\Sphere.o obj\Debug\res.o -static-libstdc++ -static-libgcc -static -lgdi32 -luser32 -lkernel32 -lcomctl32 -lopengl32 -lglu32 -mwindows
配置tasks.json和launch.json两个配置文件:
tasks.json用于编译C++文件,注意这里用到了资源文件,所以要用多任务编译。
{
"tasks":[
{
"type":"cppbuild",
"label":"g++step1",
"command": "C:\\Projects\\...\\g++.exe",
"args": [
//注意:下面的参数和前面的命令行中的参数是对应的
"-Wall",
"-fexceptions",
"-finput-charset=GBK",
"-g",
"-finput-charset=UTF-8",
"-c",
"${workspaceFolder}\\main.cpp",
"-o",
"${workspaceFolder}\\obj\\main.o"
],
"options":{
"cwd":"${fileDirname}"
},
"problemMatcher":{
"$gcc"
},
"group":{
"kind":"build",
"isDefault":true
},
"default":"调试器生成的任务"
},
{
"type":"cppbuild",
"label":"g++step2",
"command": "C:\\Projects\\...\\g++.exe",
"args": [
"-Wall",
"-fexceptions",
"-finput-charset=GBK",
"-g",
"-finput-charset=UTF-8",
"-c",
"${workspaceFolder}\\Sphere.cpp",
"-o",
"${workspaceFolder}\\obj\\Sphere.o"
],
"options":{
"cwd":"${fileDirname}"
},
"problemMatcher":{
"$gcc"
},
"group":{
"kind":"build",
"isDefault":true
},
"default":"调试器生成的任务"
},
{
"type":"cppbuild",
"label":"g++step3",
//注意:如果你有资源文件要编译的话,要用Mingw下的windres.exe
//g++.exe不能用来编译资源文件
"command": "C:\\Projects\\...\\windres.exe",
"args": [
"${workspaceFolder}\\main.rc",
"${workspaceFolder}\\obj\\res.o"
],
"options":{
"cwd":"${fileDirname}"
},
"problemMatcher":{
"$gcc"
},
"group":{
"kind":"build",
"isDefault":true
},
"default":"调试器生成的任务"
},
{
"type":"cppbuild",
"label":"g++step4",
"dependsOrder":"sequence",
"dependsOn":[
"g++Step1",
"g++Step2",
"g++Step3"
],
"command": "C:\\Projects\\...\\g++.exe",
"args": [
"-o",
"${workspaceFolder}\\bin\\Debug\\GLSL.exe",
"${workspaceFolder}\\obj\\main.o",
"${workspaceFolder}\\obj\\Sphere.o",
"${workspaceFolder}\\obj\\res.o",
"-static-libstdc++",
"-static-libgcc",
"-static",
"-lgdi32",
"-luser32",
"-lkernel32",
"-lcomctl32",
"-lopengl32",
"-lglu32",
"-mwindows"
],
"options":{
"cwd":"${fileDirname}"
},
"problemMatcher":{
"$gcc"
},
"group":{
"kind":"build",
"isDefault":true
},
"default":"调试器生成的任务"
}
],
"version": "2.0.0"
}
launch.json用于在文件编译后,启动调试程序
{
"version": "0.2.0",
"configurations": [
{
"name":"(gdb)启动",
"type":"cppdbg",
"request":"launch",
//编译后链接生成的可执行文件,也就是调试的对象
"program":"${workspaceFolder}\\bin\\Debug\\GLSL.exe",
"args":[],
"stopAtEntry":false,
"cwd":"${workspaceFolder}",
"environment":[],
"externalConsole",true,
"MIMode":"gdb",
//调试程序gdb.exe的路径,我用的编译器是Mingw,在对应的路径下找到exe文件,然后将路径填写到下面
"miDebuggerPath":"C:\\Projects\\...\\gdb.exe",
//这里填写的是前面链接生成exe文件的步骤,对应该步骤的label
//注意前面的步骤有顺序关系,如果资源文件没有链接的化,会导致生成失败
"preLaunchTask":"g++Step4",
"setupCommands":[
{
"description":"为gdb启用整齐打印",
"text":"-enable-pretty-printing",
"ignoreFailures":true
},
{
"description":"将反汇编分格设置为Intel",
"text":"-gdb-set disassembly-flavor intel",
"ignoreFailures":true
}
]
}
]
}
这两个文件配置好以后,点击运行和测试,再点击(gdb)启动旁边的三角按钮,就可以编译并进入调试界面了。正常的话,在终端会显示:
正在启动生成...
g++.exe -o bin\Debug\GLSL.exe obj\Debug\main.o obj\Debug\Sphere.o obj\Debug\res.o -static-libstdc++ -static-libgcc -static -lgdi32 -luser32 -lkernel32 -lcomctl32 -lopengl32 -lglu32 -mwindows
生成已经成功完成。
恭喜你,设置好断点你就可以调试C++了。