0

I wanted to start learning C++, but I noticed that debugging it in VSCode is kind of annoying to set up. On the Tutorial-Page it said I should start it via the developer console. Since I didnt want to do that, I searched a little and ended up with this solution:

tasks.json:


{
    "version": "2.0.0",
    "windows": {
        "options": {
            "shell": {
                "executable": "cmd.exe",
                "args": [
                    "/C",
                    "\"C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/Common7/Tools/VsDevCmd.bat\"",
                    "&&"
                ]
            }
        }
    },
    "tasks": [
        {
            "type": "shell",
            "label": "cl.exe build active file",
            "command": "cl.exe",
            "args": [
                "/Zi",
                "/EHsc",
                "/Fe:",
                "${fileDirname}\\${fileBasenameNoExtension}.exe",
                "${file}"
            ],
            "problemMatcher": [
                "$msCompile"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

launch.json:

{
    "version": "2.0.0",
    "windows": {
        "options": {
            "shell": {
                "executable": "cmd.exe",
                "args": [
                    "/C",
                    "\"C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/Common7/Tools/VsDevCmd.bat\"",
                    "&&"
                ]
            }
        }
    },
    "configurations": [
        {
            "name": "Debug",
            "type": "cppvsdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "console": "integratedTerminal",
            "preLaunchTask": "cl.exe build active file"
        }
    ]
}

This does work, but only if there are no spaces in the path. If there are, it gives this error:

Der Befehl "C:/Program" ist entweder falsch geschrieben oder konnte nicht gefunden werden.

(Sorry about the german, heres the translation: The command "C:/Program" is either not typed correctly or does not exist.)

I have tried to fix this but I just didnt get the issue. Any help is appreciated!

LightJack05
  • 119
  • 5
  • Since stackoverflow is an [English-only platform](https://meta.stackexchange.com/questions/13676/do-posts-have-to-be-in-english-on-stack-exchange), could you translate your error? – TYeung Aug 28 '21 at 17:09
  • Use quotation marks in program like you are using them in args. – 273K Aug 28 '21 at 17:10
  • @S.M. What line do you mean? – LightJack05 Aug 28 '21 at 17:12
  • you don't need to put quotes inside your `args` strings (they are probably the cause of your problem). As you have visual studio installed why not just use that? It's much easier to setup. If you really want to use vs-code try following the [documentation](https://code.visualstudio.com/docs/cpp/config-msvc) – Alan Birtles Aug 28 '21 at 17:24
  • @AlanBirtles You mean in the launch.json? That didnt work... As for VS, I could, but I just think its a little too "heavy" if you know what I mean... Also its easier to switch OSes and have a familiar environment. (No VS on Linux afaik.) – LightJack05 Aug 28 '21 at 17:30
  • Try this link for representing c:\program files without spaces. However, this won't help in cases there are spaces in the rest of the path. https://stackoverflow.com/questions/892555/how-do-i-specify-c-program-files-without-a-space-in-it-for-programs-that-cant – Gonen I Aug 28 '21 at 17:39
  • @GonenI This does work for Program Files, but not for the other folders. I still dont get why this issue only occours when I have a space in the path to the .cpp file, even though this does not change anything with this command. The path to the dev console is always the same, and it errors out on that. Any idea why this is happening in the first place? – LightJack05 Aug 28 '21 at 18:00
  • @LightJack05 Try to change the paths to the suggestions given at the bottom of this answer: https://stackoverflow.com/questions/52025194/vs-code-debugging-fails-when-theres-a-space-in-the-source-path And this is not an answer, but let me share with you that debugging in VS is about 10 times better than with VS Code. – Gonen I Aug 28 '21 at 18:08
  • Sorry, didnt work either... – LightJack05 Aug 28 '21 at 18:19
  • If you don't like setting up VS Code, I recommend using an Editor (like NotePad++), a console window and a *makefile*. There are other IDEs, like Eclipse and Netbeans. – Thomas Matthews Aug 28 '21 at 18:32

1 Answers1

0

First of all VSCode isn't good for C++

  1. You need the C++ Extension
  2. Try to not use the tasks.json
  3. Recreate the launch.json and try using this "program": "${workspaceFolder}/a.out"
  4. When comiling with g++ you need to add the -g flag
deanqx
  • 75
  • 1
  • 7