react-native在mac的m2芯片下,pod install安装glog的时候报错
一,报错内容
Installing glog (0.3.5)
[!] /bin/bash -c
set -e
#!/bin/bash
# Copyright (c) Facebook, Inc. and its affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
# pod 'Flipper-Glog', :podspec => 'https://raw.githubusercontent.com/facebook/flipper/main/pkg/specs/Flipper-Glog.podspec'
set -ePLATFORM_NAME="${PLATFORM_NAME:-iphoneos}"
CURRENT_ARCH="${CURRENT_ARCH}"
# wget -O config.guess 'https://git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.guess;hb=HEAD'
# wget -O config.sub 'https://git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.sub;hb=HEAD'if [ -z "$CURRENT_ARCH" ] || [ "$CURRENT_ARCH" == "undefined_arch" ]; then# Xcode 10 beta sets CURRENT_ARCH to "undefined_arch", this leads to incorrect linker arg.# it's better to rely on platform name as fallback because architecture differs between simulator and deviceif [[ "$PLATFORM_NAME" == *"simulator"* ]]; thenCURRENT_ARCH="x86_64"elseCURRENT_ARCH="armv7"fi
fiexport CC="$(xcrun -find -sdk $PLATFORM_NAME cc) -arch $CURRENT_ARCH -isysroot $(xcrun -sdk $PLATFORM_NAME --show-sdk-path)"
export CXX="$CC"# Remove automake symlink if it exists
if [ -h "test-driver" ]; thenrm test-driver
fi./configure --host arm-apple-darwin# Fix build for tvOS
cat << EOF >> src/config.h/* Add in so we have Apple Target Conditionals */
#ifdef __APPLE__
#include <TargetConditionals.h>
#include <Availability.h>
#endif/* Special configuration for AppleTVOS */
#if TARGET_OS_TV
#undef HAVE_SYSCALL_H
#undef HAVE_SYS_SYSCALL_H
#undef OS_MACOSX
#endif/* Special configuration for ucontext */
#undef HAVE_UCONTEXT_H
#undef PC_FROM_UCONTEXT
#if defined(__x86_64__)
#define PC_FROM_UCONTEXT uc_mcontext->__ss.__rip
#elif defined(__i386__)
#define PC_FROM_UCONTEXT uc_mcontext->__ss.__eip
#endif
EOF# Prepare exported header include
EXPORTED_INCLUDE_DIR="exported/glog"
mkdir -p exported/glog
cp -f src/glog/log_severity.h "$EXPORTED_INCLUDE_DIR/"
cp -f src/glog/logging.h "$EXPORTED_INCLUDE_DIR/"
cp -f src/glog/raw_logging.h "$EXPORTED_INCLUDE_DIR/"
cp -f src/glog/stl_logging.h "$EXPORTED_INCLUDE_DIR/"
cp -f src/glog/vlog_is_on.h "$EXPORTED_INCLUDE_DIR/"checking for a BSD-compatible install... /opt/homebrew/bin/ginstall -c
checking whether build environment is sane... yes
checking for arm-apple-darwin-strip... no
checking for strip... strip
checking for a thread-safe mkdir -p... /opt/homebrew/bin/gmkdir -p
checking for gawk... no
checking for mawk... no
checking for nawk... no
checking for awk... awk
checking whether make sets $(MAKE)... yes
checking whether make supports nested variables... yes
checking for arm-apple-darwin-gcc... /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc -arch armv7 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS18.4.sdk
checking whether the C compiler works... no
/Users/huangchuanbiao/Library/Caches/CocoaPods/Pods/External/glog/2263bd123499e5b93b5efe24871be317-6431b/missing: Unknown `--is-lightweight' option
Try `/Users/huangchuanbiao/Library/Caches/CocoaPods/Pods/External/glog/2263bd123499e5b93b5efe24871be317-6431b/missing --help' for more information
configure: WARNING: 'missing' script is too old or missing
configure: error: in `/Users/huangchuanbiao/Library/Caches/CocoaPods/Pods/External/glog/2263bd123499e5b93b5efe24871be317-6431b':
configure: error: C compiler cannot create executables
See `config.log' for more details
二,查看原因
查看了config.log文件,存在两个主要的报错,一个是ruby路径获取不到.
执行了如下策略后再次pod install 不再报错ruby路径获取不到.
brew install ruby@3.1
echo 'export PATH="/opt/homebrew/opt/ruby@3.1/bin:$PATH"' >> ~/.zshrc
echo 'export LDFLAGS="-L/opt/homebrew/opt/ruby@3.1/lib"' >> ~/.zshrc # 修复链接器路径
echo 'export CPPFLAGS="-I/opt/homebrew/opt/ruby@3.1/include"' >> ~/.zshrc # 修复编译器头文件路径
source ~/.zshrc # 立即生效
三,第二个原因
架构不兼容(armv7 与 SDK 不匹配)
我的电脑是 M2 芯片(arm64 架构),但 glog 编译脚本强制指定了 armv7 架构(旧架构,多用于 iPhone 5 及更早设备)。
你使用的 Xcode SDK(iPhoneOS18.4.sdk)可能已移除对 armv7 架构的支持(苹果近年已淘汰该架构,最新 SDK 通常只支持 arm64),导致链接器找不到 armv7 版本的系统库(如 libSystem.tbd)。
修改编译架构为 arm64
需要调整 glog 的编译脚本,将架构从 armv7 改为 arm64(适配 M2 芯片和最新 SDK),具体操作如下:
在ios目录下的podfile文件增加这个代码的处理,让强制用arm64
installer.pods_project.targets.each do |target|if target.name == 'glog'target.build_configurations.each do |config|# 核心:强制 arm64 架构config.build_settings['ARCHS'] = 'arm64'config.build_settings['VALID_ARCHS'] = 'arm64'# 适配模拟器(M2 模拟器用 arm64,排除 x86_64)config.build_settings['EXCLUDED_ARCHS[sdk=iphonesimulator*]'] = 'x86_64'# 可选:只编译当前活跃架构(加速编译)config.build_settings['ONLY_ACTIVE_ARCH'] = 'YES'endendend
然后这个ios-configure-glob.sh文件也改一下:
if [ -z "$CURRENT_ARCH" ] || [ "$CURRENT_ARCH" == "undefined_arch" ]; then# Xcode 10 beta sets CURRENT_ARCH to "undefined_arch", this leads to incorrect linker arg.# it's better to rely on platform name as fallback because architecture differs between simulator and deviceif [[ "$PLATFORM_NAME" == *"simulator"* ]]; thenCURRENT_ARCH="x86_64"elseCURRENT_ARCH="arm64"//这里改成arm64fi
fi
然后清除缓存/重启电脑,再pod install就可以了.