如何在搭载macOS 13的M1 Mac上用Clang 14编译Bash 4?
编译旧版Bash 4遇到隐式函数声明错误的解决办法
问题背景
我克隆了最新的Bash源码(ec8113b9 * Bash-5.2 patch 15: fix too-aggressive optimizing forks out of subshell commands),通过以下命令编译成功:
cd /tmp git clone https://git.savannah.gnu.org/git/bash.git cd bash ./configure && make
但尝试编译Bash 4时,执行了以下操作:
git checkout -- . # 清除第一次编译生成的文件 git checkout 64447609 # bash-5.0之前的提交,即最后一个Bash 4版本的提交 ./configure && make
编译报错:
/usr/homes/chet/src/bash/src/parse.y:5741:21: error: implicit declaration of function 'count_all_jobs' is invalid in C99 [-Werror,-Wimplicit-function-declaration] temp = itos (count_all_jobs ()); ^ 1 error generated. make: *** [y.tab.o] Error 1
尝试使用CFLAGS="-std=c89" ./configure && make,仍出现相同错误。
我的编译器版本:
$ clang --version Apple clang version 14.0.0 (clang-1400.0.29.202) Target: arm64-apple-darwin22.3.0 Thread model: posix InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
解决方法
方法1:禁用隐式函数声明的错误检查
现代clang默认将隐式函数声明视为错误,旧版Bash代码中缺少count_all_jobs的函数声明,导致编译失败。可以通过CFLAGS参数关闭该警告:
# 先彻底清理残留编译文件 git checkout -- . git checkout 64447609 make distclean # 带参数编译 CFLAGS="-Wno-implicit-function-declaration" ./configure && make
方法2:手动添加函数声明
编辑src/parse.y文件,在第5741行调用count_all_jobs之前的位置,添加函数声明:
extern int count_all_jobs(void);
保存文件后,重新执行make即可。
内容的提问来源于stack exchange,提问作者user3064538
相关产品推荐
相关产品推荐

