zsh执行报错:.out文件不存在,Kali及Windows下无法运行C/C++程序求助
C/C++代码在VS Code中运行失败的解决办法
先排查编译器本身是否正常
不管是Windows还是Kali,先脱离VS Code用终端手动编译,确认工具链没问题:
- Kali Linux:
- 打开终端输入
gcc --version,没输出就先装编译器:sudo apt update && sudo apt install gcc g++ - 写个简单的
test.c文件:#include <stdio.h> int main() { printf("Hello\n"); return 0; } - 编译:
gcc test.c -o test,运行./test,能输出Hello就说明编译器正常,问题出在VS Code配置。
- 打开终端输入
- Windows:
- 打开cmd输入
gcc --version,提示找不到命令的话,把MinGW的bin目录(比如C:\mingw64\bin)加到系统环境变量PATH,重启cmd和VS Code再试。 - 同样写
test.c,编译:gcc test.c -o test.exe,运行test.exe,能输出就说明编译器正常。
- 打开cmd输入
修正VS Code的配置文件
你的JSON配置大概率是跨系统没改对,重点调整这两个文件:
tasks.json(编译任务)
- Kali:编译命令用
gcc,输出文件不带.exe:{ "version": "2.0.0", "tasks": [ { "label": "gcc build active file", "type": "shell", "command": "gcc", "args": [ "-g", "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}" ], "group": { "kind": "build", "isDefault": true }, "problemMatcher": ["$gcc"] } ] } - Windows:编译命令用
gcc.exe,输出带.exe:{ "version": "2.0.0", "tasks": [ { "label": "gcc build active file", "type": "shell", "command": "gcc.exe", "args": [ "-g", "${file}", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe" ], "group": { "kind": "build", "isDefault": true }, "problemMatcher": ["$gcc"] } ] }
launch.json(调试配置)
- Kali:program指向无后缀的可执行文件:
{ "version": "0.2.0", "configurations": [ { "name": "gcc - Build and debug active file", "type": "cppdbg", "request": "launch", "program": "${fileDirname}/${fileBasenameNoExtension}", "args": [], "stopAtEntry": false, "cwd": "${fileDirname}", "environment": [], "externalConsole": false, "MIMode": "gdb", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ], "preLaunchTask": "gcc build active file", "miDebuggerPath": "/usr/bin/gdb" } ] } - Windows:program指向.exe文件,miDebuggerPath指向MinGW里的gdb.exe:
{ "version": "0.2.0", "configurations": [ { "name": "gcc.exe - Build and debug active file", "type": "cppdbg", "request": "launch", "program": "${fileDirname}\\${fileBasenameNoExtension}.exe", "args": [], "stopAtEntry": false, "cwd": "${fileDirname}", "environment": [], "externalConsole": false, "MIMode": "gdb", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ], "preLaunchTask": "gcc build active file", "miDebuggerPath": "C:\\mingw64\\bin\\gdb.exe" } ] }
其他注意事项
- 别把Windows的配置直接复制到Kali用,两个系统的路径分隔符(Windows是
\,Linux是/)、可执行文件后缀完全不同。 - 提示“找不到.exe”时,先看VS Code终端的错误输出,很多时候是编译没成功,根本没生成文件,错误信息会告诉你是语法问题还是编译器没找到。
- 确保VS Code的C/C++扩展是最新版,旧版本可能有兼容性问题。
内容的提问来源于stack exchange,提问作者chain
相关产品推荐
相关产品推荐

