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

macOS 14 Sonoma下安装Nokogiri 1.6.8.1编译报错求助

解决Sonoma系统安装nokogiri 1.6.8.1的编译错误方案

核心问题分析

该错误源于Sonoma自带的Clang编译器(Xcode命令行工具)对函数指针类型的检查规则更严格,而旧版nokogiri的C代码未适配新规则。即便添加-Wno-incompatible-function-pointer-types参数无效,大概率是因为该版本nokogiri的构建脚本未正确传递编译参数,或参数被后续流程覆盖。

具体解决步骤

1. 降级Xcode命令行工具到兼容版本

Sonoma默认的高版本Xcode命令行工具对旧C代码兼容性差,可降级至Xcode 14.3.1版本:

  • 下载对应架构(Intel/M2)的Xcode 14.3.1安装包
  • 安装后执行以下命令切换工具链:
    sudo xcode-select -s /Applications/Xcode_14.3.1.app/Contents/Developer
    
  • 重新尝试gem install nokogiri -v '1.6.8.1'或bundle install

2. 强制传递编译参数并绑定系统库

直接指定libxml2、libxslt路径,同时关闭函数指针类型检查的错误告警:

gem install nokogiri -v '1.6.8.1' -- \
  --with-cflags="-Wno-error=incompatible-function-pointer-types -Wno-incompatible-function-pointer-types" \
  --with-xml2-include=/usr/include/libxml2 \
  --with-xml2-lib=/usr/lib \
  --with-xslt-include=/usr/include/libxslt \
  --with-xslt-lib=/usr/lib

若使用bundle,先在Gemfile中声明版本:

gem 'nokogiri', '1.6.8.1'

再配置编译参数并安装:

bundle config build.nokogiri "--with-cflags='-Wno-error=incompatible-function-pointer-types -Wno-incompatible-function-pointer-types' --with-xml2-include=/usr/include/libxml2 --with-xml2-lib=/usr/lib --with-xslt-include=/usr/include/libxslt --with-xslt-lib=/usr/lib"
bundle install

3. 手动修改nokogiri源码后本地编译

如果上述方法无效,可直接修改源码适配编译器:

  • 下载源码包:gem fetch nokogiri -v '1.6.8.1'
  • 解压后进入目录,找到ext/nokogiri/xml_document.c文件,修改第46行的st_foreach调用,强制转换回调函数类型:
    st_foreach(node_hash, (st_foreach_callback_func*)dealloc_node_i, (st_data_t)doc);
    
  • 本地编译安装:gem build nokogiri.gemspec,再执行gem install nokogiri-1.6.8.1.gem

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 06:20:54