代码之家  ›  专栏  ›  技术社区  ›  Brian D

禁用distutils扩展的链接步骤

  •  0
  • Brian D  · 技术社区  · 7 年前

    可以禁用创建共享对象吗 distutils.core.Extension g++ -c ...

    .o 汇编

    $ python setup.py build
    running build
    ....
    building 'foo' extension
    swigging src/foobar.i to src/foobar.cpp
    swig -python -c++ -o src/foobar.cpp src/foobar.i
    

    我想到此为止,但它还在继续。

    creating build/temp.linux-x86_64-2.7
    creating build/temp.linux-x86_64-2.7/src
    gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -Isrc -I/usr/include/python2.7 -c src/foobar.cpp -o build/temp.linux-x86_64-2.7/src/foobar.o
    g++ -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro build/temp.linux-x86_64-2.7/src/foobar.o -o build/lib.linux-x86_64-2.7/foobar.so
    

    我需要直接使用CCompiler类吗?或者有没有办法解决这个问题 Extension

    23     ext_modules=[
    24         # Swig
    25         Extension(
    26             name='foobar',
    27             sources=['src/foobar.i'],
    28             include_dirs=['src'],
    29             swig_opts=['-c++'],
    30         ),
    31     ]
    
    2 回复  |  直到 7 年前
        1
  •  0
  •   Brian D    7 年前

    在不修改基础的情况下,不可能停止链接步骤 ccompiler 对象理论上可以推翻 link_shared_object 基础设施的功能 ccompiler build_ext source ).

    然而,为了回答这个问题背后的原意,可以将C/C++文件与Swig接口文件一起传递到扩展,而不需要独立编译它们并在以后链接。没有必要将swig文件生成和库编译分开。

        2
  •  0
  •   SpeqZ    6 年前

    from distutils.command import build_ext
    
    def cmd_ex(command_subclass):
        orig_ext = command_subclass.build_extension
    
        def build_ext(self, ext):
            sources = self.swig_sources(list(ext.sources), ext)
    
        command_subclass.build_extension = build_ext
        return command_subclass
    
    @cmd_ex
    class build_ext_ex(build_ext):
        pass
    
    setup(
        name = ...,
        cmdclass = {'build_ext': build_ext_ex},
        ext_modules = ...
    )
    

    覆盖distutils命令的默认行为。

    Setuptools – run custom code in setup.py