在 Visual Studio Code (VSCode) 中联合调试两个 C++ 工程,其中一个是进程,另一个是依赖库,你可以通过设置调试配置文件和使用 launch.json
来实现。
假设你的工程结构如下:
ProjectA
: 包含主进程(可执行文件)。ProjectB
: 包含库文件,ProjectA
依赖它。
步骤 1:配置 tasks.json
首先,确保你能够编译这两个项目。你需要在 tasks.json
文件中设置编译任务。
例如,对于 ProjectA
和 ProjectB
,你可以有类似的配置:
{"version": "2.0.0","tasks": [{"label": "build ProjectA","type": "shell","command": "g++","args": ["-g", "projectA_main.cpp", "-o", "projectA_executable", "-L./projectB", "-lprojectB"],"group": {"kind": "build","isDefault": true}},{"label": "build ProjectB","type": "shell","command": "g++","args": ["-g", "projectB_lib.cpp", "-o", "libprojectB.a", "-c"],"group": {"kind": "build","isDefault": true}}]
}
步骤 2:配置 launch.json
接下来,配置 launch.json
来设置联合调试。你需要为 ProjectA
(进程)创建调试配置,并确保它在启动时能够加载 ProjectB
的库。
{"version": "0.2.0","configurations": [{"name": "Debug ProjectA with ProjectB","type": "cppdbg","request": "launch","program": "${workspaceFolder}/projectA_executable", // 指向 ProjectA 的可执行文件"args": [],"stopAtEntry": false,"cwd": "${workspaceFolder}","environment": [],"externalConsole": false,"MIMode": "gdb","setupCommands": [{"description": "Enable pretty-printing for gdb","text": "-enable-pretty-printing","ignoreFailures": true}],"miDebuggerPath": "/usr/bin/gdb","preLaunchTask": "build ProjectA", // 编译 ProjectA"postDebugTask": "","logging": {"moduleLoad": false,"programOutput": true,"engineLogging": false,"exceptions": true},"windows": {"MIMode": "gdb","miDebuggerPath": "C:/path/to/gdb.exe"}}]
}
步骤 3:设置调试依赖库
如果 ProjectA
依赖于 ProjectB
库,你可以通过 launch.json
设置 LD_LIBRARY_PATH
或其他环境变量来确保调试时可以找到 ProjectB
的库。
例如,在 launch.json
中设置 environment
字段:
"environment": [{"name": "LD_LIBRARY_PATH","value": "${workspaceFolder}/projectB"}
]
步骤 4:启动联合调试
- 在 VSCode 中选择调试配置 “Debug ProjectA with ProjectB”。
- 使用
F5
或点击调试按钮开始调试。VSCode 会自动构建并启动ProjectA
,并且会连接到ProjectB
的库,开始联合调试。
这样,你就可以同时调试 ProjectA
的进程和 ProjectB
的库了。