构建一个python模块并将其链接到MacOSX框架 [英] Building a python module and linking it against a MacOSX framework

查看:123
本文介绍了构建一个python模块并将其链接到MacOSX框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在MacOSX 10.6上构建Python扩展并将其链接到多个框架(仅适用于i386).我使用distutils和Extension对象制作了setup.py文件.

I'm trying to build a Python extension on MacOSX 10.6 and to link it against several frameworks (i386 only). I made a setup.py file, using distutils and the Extension object.

我要链接到我的框架,我的 LDFLAGS env var应该如下所示:

I order to link against my frameworks, my LDFLAGS env var should look like :

LDFLAGS = -lc -arch i386 -framework fwk1 -framework fwk2

由于我在扩展模块文档中找不到任何框架"关键字,因此我改用了 extra_link_args 关键字.

As I did not find any 'framework' keyword in the Extension module documentation, I used the extra_link_args keyword instead.

Extension('test',
define_macros = [('MAJOR_VERSION', '1'), ,('MINOR_VERSION', '0')],
include_dirs = ['/usr/local/include', 'include/', 'include/vitale'],
extra_link_args = ['-arch i386',
                   '-framework fwk1',
                   '-framework fwk2'],
sources = "testmodule.cpp",
language = 'c++' )

一切都在编译和链接中.如果我从extra_link_args中删除-framework行,则链接器将按预期失败.这是python setup.py build产生的最后两行:

Everything is compiling and linking fine. If I remove the -framework line from the extra_link_args, my linker fails, as expected. Here is the last two lines produced by a python setup.py build :

/usr/bin/g++-4.2 -arch x86_64 -arch i386 -isysroot /
-L/opt/local/lib -arch x86_64 -arch i386 -bundle
-undefined dynamic_lookup build/temp.macosx-10.6-intel-2.6/testmodule.o
-o build/lib.macosx-10.6-intel-2.6/test.so
-arch i386 -framework fwk1 -framework fwk2

不幸的是,我刚刚生成的.so无法找到此框架提供的几个符号.我试图用otool检查链接的框架.他们都没有出现.

Unfortunately, the .so that I just produced is unable to find several symbols provided by this framework. I tried to check the linked framework with otool. None of them is appearing.

$ otool -L test.so
test.so:
    /usr/lib/libstdc++.6.dylib (compatibility version 7.0.0, current version 7.9.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 125.0.1)

otool的输出运行在测试二进制文件上,该文件使用我的文章顶部介绍的LDFLAGS由g ++和ldd制成.在此示例中,-framework确实起作用.

There is the output of otool run on a test binary, made with g++ and ldd using the LDFLAGS described at the top of my post. On this example, the -framework did work.

$ otool -L vitaosx 
vitaosx:
    /Library/Frameworks/fwk1.framework/Versions/A/fwk1 (compatibility version 1.0.0, current version 1.0.0)
    /Library/Frameworks/fwk2.framework/Versions/A/fwk2 (compatibility version 1.0.0, current version 1.0.0)
    /usr/lib/libstdc++.6.dylib (compatibility version 7.0.0, current version 7.9.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 125.0.1)

此问题是否可以链接到链接步骤上的"-undefined dynamic_lookup"标志?我对在Google上找到的几行文档感到有些困惑.

May this issue be linked to the "-undefined dynamic_lookup" flag on the linking step ? I'm a little bit confused by the few lines of documentation that I'm finding on Google.

干杯

推荐答案

这与未定义的dynamic_lookup无关,而与distutils无关.它将extra_link_flags附加到为python构建选择的链接标志.相反,它应该放在前面,因为-framework列表必须位于在cmdline上使用它们的对象之前(这是gcc如何收集链接符号的原因).我个人使用的一个快速修复程序是

This has nothing to do with the undefined dynamic_lookup but all with distutils. It appends the extra_link_flags to the link flags it chooses for python building. Instead it should prepend it because the -framework listings must come before the objects that use them on the cmdline (AFAIK this is due how gcc gathers symbols for linking). A quick fix that I personally use is building with

    LDFLAGS="-framework Carbon" python setup.py build_ext --inplace

或您需要的任何框架. LDFLAGS是distutils自己的标志的前缀.请注意,您的软件包将不能使用pip install.正确的修复只能来自distutils-恕我直言,他们应该像支持libraries一样支持frameworks.

or whatever frameworks you need. LDFLAGS is prepended to distutils own flags. Note that your package will not be pip installable. A proper fix can only come from distutils - imho they should support frameworks like they support libraries.

或者,您也可以添加

import os
os.environ['LDFLAGS'] = '-framework Carbon'

在您的setup.py中.然后,您的包裹应该是pip install able.

in your setup.py. Your package should then be pip installable.

这篇关于构建一个python模块并将其链接到MacOSX框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆