You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Mac下VSCode运行C程序二次执行提示zsh: killed问题求助

关于Mac上C程序二次运行提示"zsh: killed"的问题解决

问题情况

在Mac系统上使用VSCode开发C语言,已安装C/C++扩展与Code Runner扩展,开启了Code Runner的"run in terminal"设置,同时安装了clang与gcc。首次编写C程序后,无论是通过Code Runner的"Run Code"还是手动编译后执行./helloWorld,都能正常运行并输出结果;但二次运行时,终端仅提示zsh: killed。

环境信息

clang版本

jonathanyang@Jons-Mac cProgramming % clang -v
Apple clang version 12.0.0 (clang-1200.0.31.1)
Target: arm64-apple-darwin23.5.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

gcc版本

jonathanyang@Jons-Mac cProgramming % gcc -v
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 12.0.0 (clang-1200.0.31.1)
Target: arm64-apple-darwin23.5.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

使用的代码示例

#include <stdio.h>
#include <stdlib.h>

int main(){
    char myName[] = "Jon";
    printf("%s", myName);
    return 0;
}

原因分析

zsh: killed是系统主动终止进程的提示,常见触发原因包括:

  • 程序首次运行后进程未完全退出,残留进程导致资源冲突;
  • 可执行文件权限异常,系统拒绝二次执行;
  • 旧版本编译器与当前macOS Sonoma(darwin23.5.0)存在兼容性问题;
  • 系统安全机制(如Sandbox)误判程序行为。

解决方法

  1. 清理残留进程
    执行命令查看是否有残留的helloWorld进程:

    ps aux | grep helloWorld
    

    若存在对应进程,用kill -9 <进程ID>强制终止,再重新运行程序。

  2. 检查并修复可执行文件权限
    查看可执行文件权限:

    ls -l ./helloWorld
    

    如果权限中无执行标识(x),添加执行权限:

    chmod +x ./helloWorld
    
  3. 重新生成可执行文件
    每次编译前删除旧的可执行文件,避免缓存冲突:

    rm ./helloWorld && clang helloWorld.c -o helloWorld
    

    也可在Code Runner设置中修改编译命令,确保自动覆盖旧文件。

  4. 升级编译器版本
    当前clang 12.0.0与macOS Sonoma版本不匹配,重新安装适配的Command Line Tools:

    xcode-select --install
    

    按照提示完成安装后,再次检查编译器版本是否更新。

  5. 排查系统安全设置(可选)
    若上述方法无效,可临时关闭系统完整性保护(SIP)测试(不推荐长期关闭):

    • 重启Mac并按住Command + R进入恢复模式;
    • 打开终端执行csrutil disable;
    • 重启后测试程序运行,问题解决后重新开启SIP(csrutil enable)。

内容的提问来源于stack exchange,提问作者Jonathan Yang

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 16:44:53