Distutils 在构建 python 扩展时部分忽略 CC 环境变量



我正在尝试安装子process32 python模块(https://github.com/google/python-subprocess32(,并且在distutils方面遇到了一些问题。该模块包含一个必须构建的 C 扩展,但是当我运行pip install .python setup.py install时,我得到以下输出:

...
creating build
creating build/temp.linux-x86_64-2.7
/non/existent/path/gcc -pthread -fPIC ... -c _posixsubprocess.c -o build/temp.linux-x86_64-2.7/_posixsubprocess.o
unable to execute '/non/existent/path/gcc': No such file or directory

显然,由于某种原因,distutils使用了错误的gcc路径。然后我尝试使用export CC=/correct/path/to/gcc手动指定 gcc 的正确路径,并得到以下输出:

building '_posixsubprocess' extension
creating build/temp.linux-x86_64-2.7
/correct/path/to/gcc -fPIC -fno-strict-aliasing -g -O2 ... -c _posixsubprocess.c -o build/temp.linux-x86_64-2.7/_posixsubprocess.o
/non/existent/path/gcc -pthread -shared ... build/temp.linux-x86_64-2.7/_posixsubprocess.o -o build/lib.linux-x86_64-2.7/_posixsubprocess.so
unable to execute '/non/existent/path/gcc': No such file or directory

原来有问题的命令现在使用正确的路径,但它仍然试图使用 gcc 的错误位置来构建共享库。是否需要指定其他环境变量来纠正此行为?

我已经和你一起尝试了完全相同的问题。我花了一些时间深入研究distutils源代码并发现了问题所在。

distutils将使用构建 Python 时的链接配置。在这种情况下,用于构建 python 的 gcc 与您用于构建扩展的 gcc 不同。

也运行以下命令,请参阅默认链接命令:

python2 "from distutils.sysconfig import get_config_var; print(get_config_var('LDSHARED'))"

您应该发现不正确的 gcc 位于配置的开头,例如:

/non/existent/path/gcc -pthread -shared

第一个解决方案

设置环境变量LDSHARED用相应的 gcc 路径覆盖它:

export LDSHARED="/correct/path/to/gcc -pthread -shared"

然后重建扩展,它应该可以工作。

第二种解决方案(可能更好(

LDSHARED配置是从lib/python2.7/_sysconfigdata.py生成时生成的文件中检索的。

您可以修改此文件,因此无需设置环境变量。


调试技能

DISTUTILS_DEBUG环境设置为激活调试模式,以便在编译失败时可以看到回溯。

我通过自己编译扩展,从setup.py中删除对扩展的引用,安装软件包,然后将编译的文件复制到正确的位置,从而解决了这个问题。我不打算将这个答案标记为已接受,因为我相信有更好的方法。

最新更新