【问题标题】:Debugging C++ with VSCode使用 VSCode 调试 C++
【发布时间】:2021-10-28 04:03:07
【问题描述】:

我想开始学习 C++,但我注意到在 VSCode 中调试它有点烦人。 在教程页面上,它说我应该通过开发人员控制台启动它。 由于我不想这样做,所以我搜索了一下,最终得到了这个解决方案:

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

这确实有效,但前提是路径中没有空格。 如果有,它会给出这个错误:

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

(对不起德语,这里是翻译: 命令“C:/Program”要么输入不正确,要么不存在。)

我试图解决这个问题,但我只是没有得到问题。 任何帮助表示赞赏!

【问题讨论】:

  • 由于stackoverflow是English-only platform,你能翻译一下你的错误吗?
  • 在程序中使用引号,就像在 args 中使用它们一样。
  • @S.M.你是什​​么意思?
  • 您不需要在args 字符串中加上引号(它们可能是您的问题的原因)。既然你已经安装了视觉工作室,为什么不直接使用它呢?设置起来要容易得多。如果你真的想使用 vs-code 试试documentation
  • @AlanBirtles 你的意思是在 launch.json 中?那没有用...至于VS,我可以,但是如果您知道我的意思,我只是觉得它有点“沉重”...而且切换操作系统和拥有熟悉的环境也更容易。 (Linux afaik 上没有 VS。)

标签: c++ debugging visual-studio-code


【解决方案1】:

首先,VSCode 不适合 C++

  1. 您需要 C++ 扩展
  2. 尽量不要使用tasks.json
  3. 重新创建launch.json并尝试使用这个"program": "${workspaceFolder}/a.out"
  4. 使用 g++ 编译时需要添加 -g 标志

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-16
    • 2020-07-13
    • 2019-05-01
    • 1970-01-01
    • 2019-05-06
    • 2023-02-26
    相关资源
    最近更新 更多