Setting up VSCode for C/C++ debugging on Window 7

2019-02-18 19:57发布

I was following instructions from here. Installed cpptools. Created tasks.json with following contents:

{
    "version": "0.1.0",
    "command": "g++",
    "isShellCommand": true,
    "showOutput": "always",
    "args": ["-g", "helloworld.c"]
}

And launch.json with following content:

{
    "version": "0.2.0",
    "configurations": [

        {
            "name": "C++ Launch (Windows)",
            "type": "cppvsdbg",
            "request": "launch",
            "program": "${workspaceRoot}/a.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceRoot}",
            "environment": [],
            "externalConsole": false,
            "windows": {
                "MIMode" : "gdb",
                "miDebuggerPath": "C:\\Mahesh\\Program Files\\mingw\\MinGW\\bin\\gdb.exe"
            }

        },
        {
            "name": "C++ Attach (Windows)",
            "program": "${workspaceRoot}/a.exe",
            "type": "cppvsdbg",
            "request": "attach",
            "processId": "${command.pickProcess}",
            "windows": {
                "MIMode" : "gdb",
                "miDebuggerPath": "C:\\Mahesh\\Program Files\\mingw\\MinGW\\bin\\gdb.exe"
            }
        }
    ]
}

When I do Ctrl+Shift+B, the code builds, generating a.exe. When I run debug, it gives following output:

--------------------------------------------------------------------------------
You may only use the C/C++ Extension for Visual Studio Code with Visual Studio
Code, Visual Studio or Xamarin Studio software to help you develop and test your
applications.
--------------------------------------------------------------------------------
Loaded 'C:\Mahesh\repos\VSCodeC\polyaddition\a.exe'. Symbols are not loaded.
Loaded 'C:\Windows\System32\ntdll.dll'. Symbols are not loaded.
Loaded 'C:\Windows\System32\kernel32.dll'. Symbols are not loaded.
Loaded 'C:\Windows\System32\KernelBase.dll'. Symbols are not loaded.
Loaded 'C:\Windows\System32\sysfer.dll'. Symbols are not loaded.
Loaded 'C:\Windows\System32\msvcr100.dll'. Symbols are not loaded.
Loaded 'C:\Windows\System32\QIPCAP64.dll'. Symbols are not loaded.
Loaded 'C:\Windows\System32\oleaut32.dll'. Symbols are not loaded.
Loaded 'C:\Windows\System32\ole32.dll'. Symbols are not loaded.
Loaded 'C:\Windows\System32\msvcrt.dll'. Symbols are not loaded.
Loaded 'C:\Windows\System32\gdi32.dll'. Symbols are not loaded.
Loaded 'C:\Windows\System32\user32.dll'. Symbols are not loaded.
Loaded 'C:\Windows\System32\lpk.dll'. Symbols are not loaded.
Loaded 'C:\Windows\System32\usp10.dll'. Symbols are not loaded.
Loaded 'C:\Windows\System32\rpcrt4.dll'. Symbols are not loaded.
Loaded 'C:\Windows\System32\imm32.dll'. Symbols are not loaded.
Loaded 'C:\Windows\System32\msctf.dll'. Symbols are not loaded.
The thread 9524 has exited with code 0 (0x0).
Hello World!!!
The program '[7876] a.exe' has exited with code 0 (0x0).

But the code is not hitting the debug point I set up in the code. You can see, it is printing "Hello World!!!". How can I configure so that it will allow me to step through the code while debugging?

Environment:

  • The program '[7876] a.exe' has exited with code 0 (0x0), configured as "i686-pc-mingw32".
  • gcc.exe (x86_64-win32-seh-rev201506, mingwpy build) 4.9.2
  • g++.exe (x86_64-win32-seh-rev201506, mingwpy build) 4.9.2

Update

  • In the discussion in the comments, I have been asked to run gcc with m32 flag as my compiler is 64-bit, it may be generating 64-bit binaries. But gcc -m32 helloworld.c gave errors like this. The comment here explains it with -m32 option. It asks to add i686-w64-mingw32/x86_64-w64-mingw32 flags while compiling. But gcc -x86_64-w64-mingw32 helloworld.c gives language not recognized error, gcc -i686-w64-mingw32 helloworld.c gives unrecognized command line option. What I am doing wrong?
  • Also this article says that debugging is currently possible only with linux but not with Windows. Is it so?

3条回答
爷、活的狠高调
2楼-- · 2019-02-18 20:20

I believe you are trying to use the VS Code debugger (cppvsdbg) instead of gdb (cppdbg.) This modified launch.json works for me with TDM-GCC and gdb as the debugger:

{
"version": "0.2.0",
"configurations": [
    {
        "name": "C++ Launch",
        "type": "cppdbg",
        "request": "launch",
        "args": [],
        "stopAtEntry": false,
        "cwd": "${workspaceRoot}",
        "environment": [],
        "externalConsole": true,

        "linux": {
            "program": "${workspaceRoot}/a.out",
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        },
        "osx": {
            "MIMode": "lldb"
        },
        "windows": {
            "miDebuggerPath": "C:\\TDM-GCC-64\\bin\\gdb.exe",
            "program": "${workspaceRoot}\\a.exe",
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    },
    {
        "name": "C++ Attach",
        "miDebuggerPath": "C:\\TDM-GCC-64\\bin\\gdb.exe",
        "type": "cppdbg",
        "request": "attach",
        "program": "${workspaceRoot}/a.exe",
        "processId": "${command:pickProcess}",
        "linux": {
            "MIMode": "gdb",
            "program": "${workspaceRoot}/a.out",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        },
        "osx": {
            "MIMode": "lldb"
        },
        "windows": {
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\TDM-GCC-64\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    }
    ]
}
查看更多
Emotional °昔
3楼-- · 2019-02-18 20:31

If you want command.PickProcess to work..

It should be a ':' not a '.' - therefore:

{
    "name": ".NET Core Attach",
    "type": "coreclr",
    "request": "attach",
    "processId": "${command:pickProcess}"
}

Should sort you out :)

查看更多
家丑人穷心不美
4楼-- · 2019-02-18 20:35

I have faced this issue before. In my case, the compiler generated a release application as default. It has no symbols for debugging.

So, please make sure that you are generated a debug app for debugging.

good luck!

查看更多
登录 后发表回答