舊版 Ruby 在 M1 上 Compile 遇到的 libffi 問題

最近因為某些原因又要把舊專案拿出來用,Ruby 2.5.5 沒想到也已經那麼久了。不知為何好像只有 Apple ARM 上的 macOS 才會有 compile 的問題。查了一下 log 發現以下訊息:


closure.c:264:14: error: implicit declaration of function ‘ffi_prep_closure’ is invalid in C99 [-Werror,-Wimplicit-function-declaration]


result = ffi_prep_closure(pcl, cif, callback, (void *)self);


看起來是找不到 ffi 的相關函式,查了一下 brew info libffi 後得到以下訊息:


libffi is keg-only, which means it was not symlinked into /opt/homebrew,
because macOS already provides this software and installing another version in
parallel can cause all kinds of trouble.


For compilers to find libffi you may need to set:
export LDFLAGS=”-L/opt/homebrew/opt/libffi/lib”
export CPPFLAGS=”-I/opt/homebrew/opt/libffi/include”


For pkg-config to find libffi you may need to set:
export PKG_CONFIG_PATH=”/opt/homebrew/opt/libffi/lib/pkgconfig”


簡單來說就是 homebrew 的 libffi 好像會跟 macOS 的衝突,需要手動設定環境變數讓 rvm make ruby 的時候可以順利使用 libffi。把上述三個 export 下完之後再次 rvm install 2.5.5 就完成啦!