【问题标题】:Set up file launch.json and tasks.json for many files .h and files .cpp (Implement of files .h)为许多文件.h和文件.cpp设置文件launch.json和tasks.json(文件.h的实现)
【发布时间】:2021-06-01 23:55:34
【问题描述】:

在我的项目中,文件的示意图是这样的:

Project
 |__.vscode
 |__main.cpp
 |__Header
    |__Waiter.h
    |__Waiter.cpp

在 main.cpp 中,我有:#include "Header/Waiter.h"

在 Waiter.cpp 中,我有:#include "Waiter.h"

我的问题是如何设置文件launch.json 以在 VSC 中使用 F5(或 Ctrl + F5)来运行和调试这个项目?

顺便说一句,有了这样的文件task.json,我可以Run built task (Ctrl + Shift + P)然后运行main.exe

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe build active file",
            "command": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
            "args": [
                "-g",
                "${fileDirname}\\Header\\**.cpp",
                "${fileDirname}\\${fileBasenameNoExtension}.cpp",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        },
    ],
    "version": "2.0.0"
}

【问题讨论】:

  • 使用 Make、CMake 或 MSBuild 或任何其他构建工具

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


【解决方案1】:

为了调试,您需要在 .vscode 文件夹中创建 launch.json

launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "g++ - Build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: g++.exe build active file",
            "miDebuggerPath": "/usr/bin/gdb"
        }
    ]
}

我尝试在 Linux 机器上做 windows 所需的更改。

注意: preLaunchTask 的值应该等于tasks.json 中的label 的值。它会在每次调试前编译代码并创建exe。

【讨论】:

    【解决方案2】:

    我不知道怎么做,但我发现了问题:

    文件tasks.json 中的标签 必须是C/C++: g++.exe build active file!。并且文件launch.json 中的preLaunchTask 是相同的。不是C/C++: g++.exe build active file,因为我们终于有了!。现在可以运行了。如果有人知道如何通过进行这样的更改来工作,请解释原因!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-26
      • 2011-03-11
      相关资源
      最近更新 更多