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

M1 Mac(macOS 15)安装Ruby 3.0.2时遇'maybe_unused'属性报错

解决M1 Mac(macOS 15)安装x86_64架构Ruby 3.0.2失败的问题

问题根源

Ruby 3.0.2是已终止维护的旧版本,其源码中的RB_UNUSED_VAR宏写法不符合macOS 15自带新版Clang编译器的语法规范,导致[[maybe_unused]]属性应用到类型时触发编译错误。同时,M1芯片上跨架构编译时,若混用arm64架构的依赖库,也可能加剧兼容性问题。

可行解决方案

方案1:添加编译标志忽略属性错误

直接通过CFLAGS传递编译器参数,跳过该类型的错误检查:

arch -x86_64 CFLAGS="-Wno-error=attributes" asdf install ruby 3.0.2

若上述命令仍报错,可更精准地指定忽略maybe_unused相关错误:

arch -x86_64 CFLAGS="-Wno-error=maybe-unused-attribute" asdf install ruby 3.0.2

方案2:使用x86_64架构的Homebrew依赖

M1 Mac上默认的Homebrew(/opt/homebrew)是arm64架构,跨架构编译x86_64 Ruby时,需搭配x86_64架构的依赖库:

  • 进入x86_64架构的Shell:
    arch -x86_64 zsh
    
  • 若未安装x86_64版本的Homebrew,执行安装脚本(会自动安装到/usr/local路径):
    /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
    
  • 安装x86_64架构的依赖:
    brew install openssl@1.1 readline libyaml gmp
    
  • 在当前x86_64 Shell中执行Ruby安装命令(可搭配方案1的编译标志):
    asdf install ruby 3.0.2
    

方案3:手动修补Ruby源码宏定义

若上述方法无效,可临时修改Ruby源码中的宏以适配新版编译器:

  • 先让ruby-build下载源码但不编译:
    arch -x86_64 asdf install ruby 3.0.2 --keep
    
  • 找到源码目录(通常在~/.asdf/plugins/ruby/ruby-build/sources/ruby-3.0.2或临时缓存目录),编辑include/ruby/backward/2/attributes.h文件:
    将#define RB_UNUSED_VAR(x) x RBIMPL_ATTR_MAYBE_UNUSED()修改为:
    #define RB_UNUSED_VAR(x) __attribute__((unused)) x
    
  • 回到源码目录执行编译安装:
    cd ~/.asdf/plugins/ruby/ruby-build/sources/ruby-3.0.2
    make clean && make -j10 && make install
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 04:57:09