vscode构建c程序时报错。错误信息如下。
Cannot build and debug because the active file is not a C or C++ source file.
The terminal process failed to launch (exit code: -1).
任务配置文件如下。
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/gcc",
"args": [
"-fdiagnostics-color=always",
"-g",
"/home/xxx/tmp/test/main.c"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
我在工作区文件夹 /home/xxx/tmp/test 下有一个名为 main.c 的 c 文件。问题的原因可能是什么?
如 Sean McManus 的 this 回复所示,您需要删除
${file}
中的所有 tasks.json
引用,例如:
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
}
更改为
"args": [
"-g",
"test.cpp",
"-o",
"libs/test.o"
],
"options": {
"cwd": "${fileDirname}"
}
并将构建类型从
cppBuild
更改为 shell
。
我也遇到了与上面相同的错误。但是,当我在主函数中标记调试并调试程序中的其他一些行时。然后,运行“开始调试(F5)”然后调试对我有用。
对于遇到类似问题的人来说有帮助的解决方案。
也许您的“在终端中运行”框未选中。
您可能想要转到 VScode 左上角的汉堡包图标 > 然后选择“文件”> 选择“首选项”> 然后单击“设置”> 从常用中选择“扩展”> 然后从那里向下滚动并打开“运行代码配置”>之后,现在进入该配置并慢慢滚动,您会发现“在终端中运行”未选中,只需选中该框即可。
之后,当您单击“运行代码”时,您的 C/C++ 代码将在终端中运行。