VS Code调试C程序时执行到main结尾跳转crtexe.c问题修复
调试C程序时main结束后无法自动终止的问题修复
在Visual Studio Code中调试C程序时遇到异常:执行到main(int argc, char** argv)函数的闭合大括号后,按下F10键不会自动结束调试,而是打开路径C:\M\mingw-w64-crt-git\src\mingw-w64\mingw-w64-crt\crt\crtexe.c下的空crtexe.c文件。
我的C代码
#include <stdio.h> int main(int argc, char** argv) { int first, second, temp; printf("Enter first number: "); scanf("%d", &first); printf("Enter second number: "); scanf("%d", &second); // value of first is assigned to temp temp = first; // value of second is assigned to first first = second; // value of temp (initial value of first) is assigned to second second = temp; // %.2lf displays number up to 2 decimal points printf("\nAfter swapping, first number = %d\n", first); printf("After swapping, second number = %d\n", second); return 0; }
我的launch.json配置
{ "version": "0.2.0", "configurations": [ { "name": "C/C++: g++ build and debug active file", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}\\a.out", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": false, "MIMode": "gdb", "miDebuggerPath": "C:\\msys64\\mingw64\\bin\\gdb.exe", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ], "preLaunchTask": "C/C++: g++ build active file" }, { "name": "(Windows) launch", "type": "cppvsdbg", "request": "launch", "program": "${workspaceFolder}/a.exe", "args": [], "stopAtEntry": false, "cwd": "${fileDirname}", "environment": [], "console": "externalTerminal" } ] }
已安装插件
- C/C++(Microsoft)
- C/C++ Extension Pack(Microsoft)
- C/C++ Themes
- CMake
- Cmake Tools
- Code Runner
修复方案
1. 修正可执行文件路径
Windows平台下MinGW编译生成的是.exe格式的可执行文件,将第一个调试配置(cppdbg类型)中的program路径从"${workspaceFolder}\\a.out"改为"${workspaceFolder}\\a.exe",确保调试器指向正确的目标程序。
2. 添加自动终止调试配置
在cppdbg类型的配置中添加"exitOnDebuggeeExit": true,该配置会让调试器在目标程序退出时自动终止调试会话,避免进入CRT的后续清理代码。修改后的配置片段如下:
{ "name": "C/C++: g++ build and debug active file", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}\\a.exe", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": false, "MIMode": "gdb", "miDebuggerPath": "C:\\msys64\\mingw64\\bin\\gdb.exe", "exitOnDebuggeeExit": true, "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ], "preLaunchTask": "C/C++: g++ build active file" }
3. 调整调试操作
当调试到main函数的闭合大括号后,使用**F5(继续运行)**代替F10(单步执行),这样程序会直接完成CRT的清理流程并退出调试会话,不会单步进入CRT的源码文件。
内容的提问来源于stack exchange,提问作者Siarhei Ionushas
相关产品推荐
相关产品推荐

