Home > database >  When debugging in Visualstudio Code, there is a transition to the crtexe.c file
When debugging in Visualstudio Code, there is a transition to the crtexe.c file

Time:09-17

I ran into a problem while debugging a C program in visual studio code, namely when I get to the closing curly brace int main(int argc, char** argv) further pressing the F10 key does not end debugging as it used to, but opens an empty crtexe.c file in this path C: > M > mingw-w64-crt-git > src > mingw-w64 > mingw-w64-crt > crt > crtexe.c

This is my simple C code program

#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;
}

My launch.json file

{
"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"
    }
]

}

How can I fix this so that after the last curly brace, my debugging automatically ends? P.S. of installed plugins I have: C/C (Microsoft), C/C Extension Pack(Microsoft), C/C Themes, CMake, Cmake Tools, Code Runner.

CodePudding user response:

The function main() runs from a context, and this is the CRT, the C run time environment. This code (commonly) fulfills the promises the C standard gives you, like providing argc and argv, initializing all static objects, preparing stdin and stdout, and so on.

Now if main() returns, be it at the closing curly brace or via other means, it returns to the CRT. (Please note that there are other possibilities, but this is a different issue.)

F10 is the shortcut for "step over", and you command the debugger to execute the next line of the current function. Since this is the last line of main(), it tries to show you the next statement after the call of main() in "crtexe.c". Since the source of library code is commonly not available in the sense of not installed, VSC shows you an empty view.

Because it is a legitimate request to step out of main(), this is the expected behavior.

If you want your debuggee to finish quietly, simply do not step out of main(). If you come to such a point, just press F5 to continue the application, and all is well.

The same applies to any attempt to step into a function of a standard library. Save yourself from frustration and avoid it.

  • Related