为什么在 Mac 上编译 Python C 扩展会自动带上 --arch i386?
我在 Mac 上编写一个 python C 扩展,setup.py 是这样的:
# file: setup.py
from distutils.core import setup, Extension
extra_objects = ["/Users/rrg/Documents/test/aliyun-log-c-sdk/build/Debug/lib/liblog_c_sdk_static.a"]
module1 = Extension(‘pycproducer’,
sources=[‘pycproducer.c’],
extra_objects=extra_objects)
setup(name=‘pycproducer’,
version=‘1.0’,
description=‘This is a Math package’,
ext_modules=[module1])
其中liblog_c_sdk_static是我要带上的静态库。但是当我运行python setup.py install的时候,报了这个错:
第一行估计是编译的命令:
cc -bundle -undefined dynamic_lookup -arch x86_64 -arch i386 -Wl,-F. build/temp.macosx-10.13-intel-2.7/pycproducer.o /Users/rrg/Documents/test/aliyun-log-c-sdk/build/Debug/lib/liblog_c_sdk_static.a -o build/lib.macosx-10.13-intel-2.7/pycproducer.so
后两行是 warning:
ld: warning: The i386 architecture is deprecated for macOS (remove from the Xcode build setting: ARCHS)
ld: warning: ignoring file /Users/rrg/Documents/test/aliyun-log-c-sdk/build/Debug/lib/liblog_c_sdk_static.a, file was built for archive which is not the architecture being linked (i386): /Users/rrg/Documents/test/aliyun-log-c-sdk/build/Debug/lib/liblog_c_sdk_static.a
我是这么理解的:
- python 的
distutils给我生成 gcc 命令,然后生成 gcc 命令里,带了-arch x86_64 -arch i386两个架构。 - 我的静态库,
liblog_c_sdk_static.a是只支持 x86_64 的,我用了lipo -info命令看过,显示: architecture: x86_64 - 然后 xcode 发现我自己的代码是
-arch x86_64 -arch i386,但是静态库只有 x86_64,于是给我告警,放弃链接静态库。
请问有大佬知道现在怎么办吗?
我理解是应该有个地方,比如在 python 的distutils里,有个地方指定屏蔽某个 architecture,或者在 gcc 的全局变量里屏蔽也行。
为什么在 Mac 上编译 Python C 扩展会自动带上 --arch i386?
在Mac上编译Python C扩展时自动带上--arch i386,这通常是因为你的Python解释器本身是32位(i386)架构的。这可能是由于你安装了一个较老版本的Python,或者通过某些包管理器(如MacPorts)安装的Python默认是32位版本。
要解决这个问题,你可以检查你的Python解释器架构,并考虑安装64位版本的Python。以下是检查Python架构的方法:
import platform
import struct
print("Platform:", platform.platform())
print("Machine:", platform.machine())
print("Architecture (bits):", struct.calcsize("P") * 8)
如果输出显示是32位架构,那么你需要安装64位的Python版本。你可以从Python官网下载64位安装包,或者使用Homebrew安装:
brew install python
安装后,确保你的环境变量指向新的Python解释器。这样在编译C扩展时就不会自动添加--arch i386参数了。
总结:检查并安装64位Python。

