0

I used to use Visual Studio 2019 for developing C and C++ programs, now I have moved to VS Code, and I find that I can't open a file with a relative path when debugging, only with absolute path it can work.

here's my launch.json and tasks.json files:

launch.json:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "gcc.exe - Build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "C:\\mingw64\\bin",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\mingw64\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: gcc.exe build active file"
        }
    ]
}

tasks.json:

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: gcc.exe build active file",
            "command": "C:\\mingw64\\bin\\gcc.exe",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "C:\\mingw64\\bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

Thank you so much for solving my problem.

StarGazerD
  • 37
  • 8
  • Check your program’s “current working directory” at startup, via [`GetCurrentDirectory()`](https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getcurrentdirectory) or equivalent. The path is likely not what you are expecting. This is why you should not be using relative paths to begin with. – Remy Lebeau Jan 22 '21 at 03:54
  • Does this answer your question? [How do I set the working directory to the "solution directory" in c++?](https://stackoverflow.com/questions/4815423/how-do-i-set-the-working-directory-to-the-solution-directory-in-c) – Jeffrey Jan 22 '21 at 03:56
  • 3
    What **exactly** is the procedure you are doing to "open a file with a relative path" that is failing? – Yakk - Adam Nevraumont Jan 22 '21 at 04:24
  • Relative paths are (well) relative to the current working directory of your program. If the current working directory of your program is not set right (e.g. to a directory that contains the various expected sub-directories) then relative paths don't work. Equally true is that absolute paths don't work if you attempt to open a file using an absolute path that does not exist. – Peter Jan 22 '21 at 04:54

1 Answers1

0

Relative paths are encountered from the current working directory of the application, and you've provided

"cwd": "C:\\mingw64\\bin",

if you want to run from the current project root directory set it to like this (or set it to where your prefer)

"cwd": "${workspaceRoot}"

Edit: if you set it in that way to be able to run gcc consider adding C:\\mingw64\\bin in your PATH variable.

thakee nathees
  • 877
  • 1
  • 9
  • 16