使用 VSCode 而不是 Visual Studio 调试 C++ DLL

Debug C++ DLL with VSCode instead of Visual Studio

我正在处理一个需要调试 C++ 的项目 DLL/Lib。我有 DLL/PDB/H/Source 个文件,我需要一个“客户端”来调用 DLL,这样我就可以调试一些功能。

到目前为止,我在 Visual Studio 中有一个“DLL 客户端”的解决方案。为了让它工作,我必须添加我的“附加包含目录”(.h 文件),“链接器”上的 dll 路径,并在“构建事件”中执行 dll/pdb 的 xcopy。 这是可行的,但我不太喜欢 visual studio 界面,想尝试 code/debug 和 VSCode。

到目前为止我的客户端非常简单:

    #include <iostream>
    #include "mydll.h"
    
    int main()
    {
        char version[20];
        GetVersion(version);
        std::cout << "Version: " << version << '\n';
    }

我的 DLL 是用 makefile 制作的。我在如何在 VSCode 上设置我的 launch.json(或设置什么)以添加包含目录和 pdb 信息时遇到问题,所以当我 运行 我的客户 dll/pdb链接到客户端。

launch.json 是 vscode 的默认值,只是名称更改了

{"configurations": [
    {
        "name": "(gdb) DLL Client",
        "type": "cppdbg",
        "request": "launch",
        "program": "${workspaceRoot}/dllclient.out",
        "args": [],
        "stopAtEntry": false,
        "cwd": "${fileDirname}",
        "environment": [],
        "externalConsole": false,
        "MIMode": "gdb",
        "miDebuggerPath": "C:\MinGW\bin\gdb.exe",
        "setupCommands": [
            {
                "text": "-enable-pretty-printing",
                "ignoreFailures": true
            }
        ]
    }
    ]
}

有什么帮助吗?谢谢

所以,我不知道它是否是最好的解决方案,但这是我能找到的解决方案。可能有一些方法可以通过 vscode.

的 C++ 扩展来改进这一点

launch.json(用于调试)

{"configurations": [
    {
        "name": "(gdb) VCVarsall 32",
        "type": "cppvsdbg",
        "request": "launch",
        "program": "${workspaceFolder}\main.exe",
        "args": [],
        "stopAtEntry": false,
        "cwd": "${fileDirname}",
        "environment": [],
        "externalConsole": false,
        "MIMode": "gdb",
        "targetArchitecture": "x86",
        "miDebuggerPath": "C:\MinGW\bin\gdb.exe",
        "preLaunchTask": "C++: vcvarsall Main 32",
    },
]}

tasks.json(构建 main 和 link 库)

{
    "tasks": [
        {
            "type": "process",
            "label": "C++: vcvarsall Main 32",
            "command": "cmd",
            "options": {"cwd": "${workspaceFolder}"},
            "args": ["/C vcvarsall x86 && cl /Od /Zi /EHsc /Fd:vc140.pdb /Fo:main.obj ./main.cpp /I ${workspaceFolder}\dev /link ${workspaceFolder}\libs\mylibrary.lib /OUT:main.exe /PDB:vc140.pdb"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
        }
    ],
    "version": "2.0.0"
}