编辑
编译
调试
Turbo C
VC6
VS系列
CODEBLOCKS
下载地址:vscode
本机采用VSCODE作为开发环境,下载安装文件后直接安装即可。
也可采用轻量级的codeblocks,下载地址:codeblocks-20.03。
使用vscode打开C测试工程所在的目录,并新建录入示例代码程序。
#include
int main()
{
char c='A';
printf("c(char)=%c,c(int)=%d,c_change(char)=%c,c_change(int)=%d,c_change(HEX)=%#x\n",c,c,c+32,c+32,c+0x20);
return 0;
}
使用vscode打开C测试工程目录中的.vscode文件夹,修改配置文件:
如果尚未生成配置文件,则需要配置默认生成任务:
在launch.json底部的 “setupCommands”: []右方括号后增加英文逗号",“,并下一行加一行代码,如:
“preLaunchTask”:“C/C++: gcc.exe”
预执行任务之后的名字一定要和tasks.json中"label"的value一致。
tasks.json中的文件,command配置指定编译器为gcc(需带完整路径):
f:\dev\rtools40\mingw32\bin\gcc.exe
args是编译器选项,其中-g指编译出带调试信息的可执行文件,” f i l e " 是要编译的文件名, − o 参数指定生成可执行文件的名字,如: " {file}"是要编译的文件名,-o参数指定生成可执行文件的名字,如: " file"是要编译的文件名,−o参数指定生成可执行文件的名字,如:"{fileDirname}/${fileBasenameNoExtension}.exe"
正常启动gdb,必须保证gcc生成的.exe可执行文件版本是否与gdb调试器版本一致,否则会出现调试错误:“File format not recognized”,如:
若采用的gdb版本为32位,则编译器也采用32位版本的gcc。
tasks.json和launch.json配置文件(32位)如下:
tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc.exe 生成活动文件",
"command": "f:\\dev\\rtools40\\mingw32\\bin\\gcc.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "f:\\dev\\rtools40\\mingw32\\bin\\"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "编译器: f:\\dev\\rtools40\\mingw32\\bin\\gcc.exe"
}
]
}
launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) 启动",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": true, //false单独打开console窗口用于程序执行
"MIMode": "gdb",
"miDebuggerPath": "F:\\dev\\rtools40\\mingw32\\bin\\gdb.exe",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "将反汇编风格设置为 Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: gcc.exe 生成活动文件"
},
{
"name": "(Windows) 启动",
"type": "cppvsdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"console": "externalTerminal"
}
]
}
正常调试后,程序停在了设定的断点部位,可执行后续的调试操作。
弹出的console窗口中显示程序执行结果:
调试控制台显示了gdb的版本及配置信息:
=thread-group-added,id="i1"
GNU gdb (GDB) 7.5
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-pc-mingw32".
可以看到GNU gdb (GDB)版本为 7.5,This GDB was configured as “i686-pc-mingw32”.32位版本。
至此,完成了例程的调试。