【问题标题】:Distutils partially ignoring CC environment variable when building python extensionDistutils在构建python扩展时部分忽略CC环境变量
【发布时间】:2017-07-25 16:04:28
【问题描述】:

我正在尝试安装 subprocess32 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 的错误位置来构建共享库。我需要指定另一个环境变量来纠正这种行为吗?

【问题讨论】:

    标签: python gcc setuptools distutils


    【解决方案1】:

    我已经和你尝试过完全相同的问题。我花了一些时间研究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环境开启调试模式,编译失败时可以看到traceback。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-02-12
        • 1970-01-01
        • 2012-09-05
        • 2016-10-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-03
        相关资源
        最近更新 更多