Home Telegram Scroll Top

macOS软件编译时找不到头文件解决方法[更新10.15]

昨天(也就是2018-09-25)新版本的macOS Mojave 10.14正式开放下载,我如此时尚,自然是要第一时间下载安装的(这句话好熟悉的样子)。

macOS Catalina 10.15 还是这个鬼样子,不过解决方法略有不同,见文章最后更新部分。

今天开发时发现有些PHP扩展无法使用,提示版本不兼容需要重新编译。

这个自然是难不倒我的,于是:下载扩展源码包、解压后进入文件夹、执行phpize报错了。。。。

$ phpize
grep: /usr/include/php/main/php.h: No such file or directory
grep: /usr/include/php/Zend/zend_modules.h: No such file or directory
grep: /usr/include/php/Zend/zend_extensions.h: No such file or directory
Configuring for:
PHP Api Version:        
Zend Module Api No:     
Zend Extension Api No:

缺少PHP的header头文件,再一看/usr/include文件夹根本不存在,想必是Xcode command line tools没有安装完全(我macOS 10.13版本装过的)?熟练的敲出如下命令:

$ xcode-select --install
xcode-select: note: install requested for command line developer tools

静等了一会安装完成,一看/usr/include文件夹还是不存在,难道升级系统后System Integrity Protection自动打开导致文件写不进?接着执行:

$ csrutil status
System Integrity Protection status: disabled.

SIP处于禁用状态,没有问题。

再安装一遍Xcode command line tools试试:

$ xcode-select --install
xcode-select: error: command line tools are already installed...

不给装了,这可如何是好。。

于是各种吃力爬英文网站,最后终于让我找到了,只需执行下列命令安装header头文件SDK即可:

$ cd /Library/Developer/CommandLineTools/Packages/
$ open macOS_SDK_headers_for_macOS_10.14.pkg

安装完成再一看/usr/include文件夹和里面的都文件都回来了。

接下里一切都简单多了,再次(编译过PHP扩展的一看就懂):

$ phpize
$ ./configure
$ make
$ sudo make install

以上就是解决问题的大概过程,然后一一顺利解决掉所有不兼容的PHP扩展,很开心。

顺便记录一下如何将自己的mac变成一台开发机器:

# csrutil disable   # 需要在恢复模式下运行命令,具体请自行搜索。
xcode-select --install    # 安装常用开发工具,如:git等。 cd /Library/Developer/CommandLineTools/Packages/
open macOS_SDK_headers_for_macOS_10.14.pkg sudo DevToolsSecurity -enable # 将系统置于开发模式

macOS 10.15 (Catalina) 更新部分(多关注man xcrun):

macOS 10.15 下/System/Library/Frameworks/也缺少很多头/库文件,会导致很多软件编译失败。
比如:CoreFoundation(找不到 #include <CoreFoundation/CoreFoundation.h>),在这里我也一并处理了。

# csrutil disable   # 需要在恢复模式下运行命令,具体请自行搜索。
xcode-select --install    # 安装常用开发工具,如:git等。 sudo mount -uw /  # 根目录挂载为可读写,否则无法在/usr/下建立文件,本修改重启前有效。
sudo ln -s "(xcrun --show-sdk-path)/usr/include" /usr/include
export SDKROOT="(xcrun --show-sdk-path)" # 设置环境变量
echo "export SDKROOT=\"\$(xcrun --show-sdk-path)\"" >> ~/.bash_profile # zsh的自行搞定 sudo DevToolsSecurity -enable # 将系统置于开发模式
分享本文到:

留下评论

您的电子邮箱地址不会被公开。 必填项已用 * 标注