如何在VisualStudio+CMake环境下调试外部程序调用的C++ DLL
用外部程序调试C++ DLL(Visual Studio + CMake)的正确配置方法
1. 修正launch.vs.json的语法与配置错误
你的配置存在两处关键问题:
defaults字段后多了无效字符1,属于JSON语法错误,直接导致配置无法被识别type字段需指定为cppvsdbg(Visual Studio专属C++调试器类型),而非default
以下是修正后的完整配置:
{ "version": "0.2.1", "defaults": {}, "configurations": [ { "name": "调试mydll.dll(外部程序)", "type": "cppvsdbg", "project": "CMakeLists.txt", "projectTarget": "mydll.dll", "program": "C:\\full\\path\\to\\external_program.exe", "args": ["path_to_input_file"], "cwd": "${workspaceRoot}", "environment": [{"name": "PATH", "value": "${workspaceRoot}\\build\\Debug;${env:PATH}"}] } ] }
2. 核心配置项说明
program:必须填写外部程序的绝对路径,避免路径解析失败environment:将DLL的编译输出目录(如Debug构建路径)添加到系统PATH,确保外部程序能找到并加载你的DLLcwd:设置外部程序的工作目录,可根据实际运行需求调整
3. 必要前置操作
- 确保CMakeLists.txt中正确定义DLL目标:
add_library(mydll SHARED src/your_source_code.cpp ) - 在DLL的导出函数或关键代码位置设置断点
- 先编译生成Debug版本的DLL,再启动调试
内容的提问来源于stack exchange,提问作者Serge Weinstock
相关产品推荐
相关产品推荐

