c++linuxwindowswindows-subsystem-for-linuxvscode-debugger

VS Code cannot find the path of C++ binary in WSL


I have a C++ project in WSL, and VS Code on the windows side. Say my C++ binary is at: /home/myhome/bin/test, then in /home/myhome, I launch the VS Code: >code . In VS Code, the launch.json is like:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "WSL: Debug C++ Project",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/bin/test",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "Build C++ Project"
        }
    ]
}

But when debugging, VS Code complains: \\wsl.localhost\Ubuntu\home\myhome: No such file or directory

I also tried:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "WSL: Debug C++ Project",
            "type": "cppdbg",
            "request": "launch",
            "program": "/home/myhome/bin/test",
            "args": [],
            "stopAtEntry": false,
            "cwd": "/home/myhome",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "Build C++ Project"
        }
    ]
}

This time, the error message is "\home\myhome\bin\test" cannot be found. I notice in the error message it becomes back slash. Does anyone know what is missing here?


Solution

  • The error occurs because VS Code is using Windows-style paths instead of Linux paths in WSL.

    1.Use the Remote-WSL extension to ensure VS Code runs inside WSL.

    2.Update launch.json with Linux paths and specify gdb's location:

    {
        "version": "0.2.0",
        "configurations": [
            {
                "name": "WSL: Debug C++",
                "type": "cppdbg",
                "request": "launch",
                "program": "/home/myhome/bin/test",
                "args": [],
                "cwd": "/home/myhome",
                "MIMode": "gdb",
                "miDebuggerPath": "/usr/bin/gdb",
                "setupCommands": [
                    {
                        "description": "Enable pretty-printing",
                        "text": "-enable-pretty-printing",
                        "ignoreFailures": true
                    }
                ],
                "preLaunchTask": "Build C++ Project"
            }
        ]
    }
    

    Steps.

    S-1.Open your project in WSL by running code . inside WSL.

    S-2.Ensure the C++ extension is installed in the WSL side of VS Code.

    S-3.Use the corrected launch.json above.