VSCode无法识别make命令,如何用Makefile构建调试MinGW-64程序?
问题:VSCode中使用Makefile构建MinGW-64 C++程序提示
make未识别 我在Visual Studio Code中调试MinGW-64 C++程序时,构建环节就出问题——VSCode无法识别make命令。我有一个Makefile,在命令行里能正常构建,但用当前的tasks.json配置执行构建时出错。
当前tasks.json配置:
{ "version": "2.0.0", "tasks": [ { "label": "C/C++: g++.exe build active file", "type": "shell", "command": "make", "options": { "cwd": "${fileDirname}" }, "problemMatcher": [ "$gcc" ], "group": { "kind": "build", "isDefault": true }, "detail": "Task generated by Debugger." } ] }
构建时的错误信息:
make : The term 'make' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
已尝试操作
- 已配置Makefile Tools扩展的两项设置:
Makefile: Makefile Path:[Project directory]\src\MakefileMakefile: Make Path:[...]\msys\mingw64\bin\mingw32-make.exe
- 命令行中输入
make或完整路径的mingw32-make.exe均能正常运行,推测VSCode内还有路径未配置到位。
解决方案
方法一:修改tasks.json指定mingw32-make路径
直接把tasks.json里的command字段替换为mingw32-make.exe的完整路径;如果系统环境变量已包含该路径,直接写mingw32-make即可:
{ "version": "2.0.0", "tasks": [ { "label": "C/C++: g++.exe build active file", "type": "shell", "command": "[...]\msys\mingw64\bin\mingw32-make.exe", // 替换为你的实际路径 "options": { "cwd": "${fileDirname}" }, "problemMatcher": [ "$gcc" ], "group": { "kind": "build", "isDefault": true }, "detail": "Task generated by Debugger." } ] }
如果命令行能运行make但VSCode不行,重启VSCode让它重新加载系统环境变量即可。
方法二:利用Makefile Tools扩展自动构建
既然已经配置了Makefile Tools扩展,无需手动维护tasks.json:
- 打开命令面板(Ctrl+Shift+P)
- 输入
Makefile: Configure,等待扩展加载项目配置 - 再输入
Makefile: Build,扩展会自动使用你配置的Make Path执行构建
调试配置(launch.json)
构建成功后,创建或修改launch.json来配置调试:
{ "version": "0.2.0", "configurations": [ { "name": "C/C++: g++.exe debug active file", "type": "cppdbg", "request": "launch", "program": "${fileDirname}/[你的可执行文件名].exe", // 替换为Makefile生成的可执行文件路径 "args": [], "stopAtEntry": false, "cwd": "${fileDirname}", "environment": [], "externalConsole": false, "MIMode": "gdb", "miDebuggerPath": "[...]\msys\mingw64\bin\gdb.exe", // 替换你的gdb实际路径 "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ], "preLaunchTask": "C/C++: g++.exe build active file" // 与tasks.json中的label保持一致,或使用Makefile Tools的构建任务标签 } ] }
内容的提问来源于stack exchange,提问作者Stunk x Chunk
相关产品推荐
相关产品推荐

