小牛队:将Python解释器编译成静态库

时间:2014-03-08 19:01:50

标签: macos python-3.x static-libraries osx-mavericks python-embedding

我想将 Python 3.3 解释器嵌入到 Mac OS 10.9 Cocoa应用程序中,以添加一些Python功能。从我从另一个StackOverflow Q& A中读到的内容来看,最好是创建一个静态库(页脚中的引用)而不是动态库。

以下是我尝试使用Python解释器创建静态库(.a文件):

  1. 我从here下载了Python 3.3(CPython)源代码。
  2. 我在*static*文件
  3. 中添加了Modules/Setup.dist
  4. 我已输入以下内容来编译终端中的源:
    • ./configure LDFLAGS="-static -static-libgcc" CPPFLAGS="-static"
  5. 我得到的结果如下:

    checking build system type... x86_64-apple-darwin13.1.0
    checking host system type... x86_64-apple-darwin13.1.0
    checking for --enable-universalsdk... no
    checking for --with-universal-archs... 32-bit
    checking MACHDEP... darwin
    checking for --without-gcc... no
    Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
    checking for gcc... gcc
    checking whether the C compiler works... no
    configure: error: in `/Path/To/My/Source/Python-3.3.4':
    configure: error: C compiler cannot create executables
    See `config.log' for more details
    

    我的理解是gcc实际上被小牛队的Apple LLVM 5.0(clang-500.2.79)(基于LLVM 3.3svn)所取代。

    另外,我在 config.log ...

    中找到了以下内容
    configure:3914: checking whether the C compiler works
    configure:3936: clang  -static  conftest.c  >&5
    ld: library not found for -lcrt0.o
    clang: error: linker command failed with exit code 1 (use -v to see invocation)
    

    问题: 如何使用 Apple LLVM 编译Python 3.3,因此我有一个静态库,例如 libpython3.3。一个


2 个答案:

答案 0 :(得分:0)

我认为默认是在基于Unix的平台上构建静态库,包括OSX。也就是说,只需简单配置,make,make install。它对我有用,并构建了libpython3.4m.a。但您可以考虑使用--prefix并阅读有关在OSX上安装多个版本的Python的信息。

您引用的主题是旧的吗?

嵌入并不是那么奇怪,否则Python文档和书籍就不会深入讨论。

答案 1 :(得分:0)

从python 3.5开始,在配置时不需要使用LDFLAGS和CPPFLAGS。

使用configure --disable-shared标志,它将构建一个静态库。

使用configure --enable-shared标志,它将构建一个动态库。

结合--prefix,您可以使用所需的所有包设置自己的python发行版。您既可以将静态python库合并到您的应用程序中,也可以使用动态库来使交互式python正常启动。