4

I want to embed the Python 3.3 interpreter into Mac OS 10.9 Cocoa app to add some Python functionality. From what I've read from another StackOverflow Q&A, it would be best to create a static library (references in footer) than a dynamic library.

Here is what I've tried to create a static library (.a file) out of the Python interpreter:

  1. I've downloaded the Python 3.3 (CPython) source code from here.
  2. I've added *static* inside the Modules/Setup.dist file
  3. I've entered the following to compile the source in the Terminal:
    • ./configure LDFLAGS="-static -static-libgcc" CPPFLAGS="-static"

The result I get is the following:

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

My understanding is that gcc is actually replaced by Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn) in Mavericks.

Also, I found the following in the 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)

Question: How can I compile Python 3.3 using Apple LLVM so I have a static library such as libpython3.3.a?


Community
  • 1
  • 1
Alerty
  • 5,945
  • 7
  • 38
  • 62
  • What do you mean by embed? Do you mean that you want to have a Python-based app bundle using your Python 3.3? Or do you mean you want to have an Obj-C (or other non-Python lang) app bundle that embeds a Python interpreter inside of it? – Ned Deily Mar 08 '14 at 19:51
  • I mean that I have an Objective-C Cocoa app bundle and I want to embed the Python interpreter to have some Python functionality into it. – Alerty Mar 08 '14 at 21:08
  • Is it unusual to want to do this? – Alerty Mar 08 '14 at 21:31
  • Not necessarily. In the first case, the standard approach is to use `py2app` to produce a Python-based app bundle. For the second case, I think you are running into problems by trying to use `-static`. Take a look at the contents of `config.log` and read the `man ld` description of `-static`. You probably don't want to use it on OS X. – Ned Deily Mar 08 '14 at 21:48
  • I found this in the config.log... ld: library not found for -lcrt0.o – Alerty Mar 08 '14 at 22:43
  • Right. Apple deliberately does not provide a static version of the C runtime library. See, for example: http://stackoverflow.com/questions/5259249/creating-static-mac-os-x-c-build – Ned Deily Mar 08 '14 at 22:50

2 Answers2

0

I think it defaults to building a static library, on Unix based platforms, including OSX. That is, just plain configure, make, make install. It worked for me and built libpython3.4m.a. But you might consider using --prefix and read about installing multiple versions of Python on OSX.

The thread you referenced is old?

Embedding is not so strange, otherwise Python documents and books wouldn't discuss it in depth.

bootchk
  • 1,926
  • 17
  • 14
0

As of python 3.5, no need to use the LDFLAGS nor CPPFLAGS at configure time.

With the configure --disable-shared flag, it will build a static library.

With the configure --enable-shared flag, it will build a dynamic library.

Combining with --prefix, you can setup you own python distribution with all the packages you want. You can have both a static python library to incorporate to your applications, and a dynamic library to let the interactive python launch properly.

jlaurens
  • 529
  • 5
  • 10