[capi-sig] Embedding basics

ecir hana ecir.hana at gmail.com
Thu Jun 28 22:35:37 CEST 2012

Hello Henrique, Mateusz,

thank you both for the replies!

Yes, I would like to compile it myself (as opposite to using the libraries
already installed).

I don't really have a problem with Python C API, ref counting, creating
Python objects in C and such stuff. My problem is perhaps two fold:

- I don't know how is it called what I need to do in order to have one big
fat executable. Do I need a library which I can statically link agains? Is
this the "libpython.a" on some systems? From what I understood, if I want
to have just one binary I have to link "statically", correct? That is, I
have to have this ".a" file (what is it called on MacOS?) because .dll (on
Windows?) is used dynamically. So, either do I need ".a" (or
MacOS equivalent) or is it possible to somehow compile Python source
together with test.c in one go, avoiding the creation of Python library? Or
do I have to do it completely different of MacOS (frameworks et al.)?

- How to build Python myself? "./configure" and "make" works but
I'm afraid to do "make install" as it might mess with pre-installed Python,
am I right? And it creates lots of .so, and no .a's. Even if I somehow
manage to build Python and test.c what if it uses some libraries? Do I have
to somehow compile and link them to the one big executable as well or is it
just matter of packing the libraries along the binary and setting the
Python find path?

On Thu, Jun 28, 2012 at 2:27 PM, Henrique <typoon at gmail.com> wrote:

> Hey Ecir,
> By reading your email, I think you might want to first learn some basic
> concepts about C programming and how a program can be linked to dynamic or
> static libraries.
> When Python is compiled in your machine, a dynamic or a static (or both)
> library built, which you use to link with your program (your test.c). If
> MacOS provides binaries for Python, I believe the libraries might alreayd
> be there, and you might not need to compile it manually. (based on what you
> are saying, you are only trying to compile it because you thought that is
> how it should be done to have your test.c file to work). If you are still
> interested in compiling it yourself, I believe there should be instructions
> with the source (perhaps an INSTALL file there, explaining it). I am not
> sure as I do not currently have the source here with me (but it should be
> as simple as running ./configure && make && make install).
> After you have Python compiled and running on your machine, you will write
> source that will use the functions provided by the Python library in your
> application, that is where your 'test.c' program comes into play.
> Some time ago I wrote some introduction blog posts about the Python C API.
> If you are interested, this might help you a little bit
> http://www.gilgalab.com.br/2011/05/03/python-c-api-first-step/
> http://www.gilgalab.com.br/2011/05/03/python-c-api-second-step/
> But again, I really think you should look up some basics on C development
> in a Unix platform :)
> Regards,
> Henrique
> On Thu, Jun 28, 2012 at 6:08 AM, ecir hana <ecir.hana at gmail.com> wrote:
>> Hello,
>> please, I have a bit of trouble grasping a few very basic concepts related
>> to Python embedding, could someone explain those to me?
>> What I try to achieve is to have single one (big) binary, which contains
>> my
>> Python script, the wrapper and Python interpreter itself, I'm on MacOS
>> 10.6. My questions are:
>> - I would like to build Python myself. I downloaded Python 3.3 beta source
>> code, extracted it in a folder. Next to "Python-3.3.0b1" folder, there is
>> a
>> file "test.c" which contains:
>> #include <Python.h>
>> int
>> main(int argc, char *argv[])
>> {
>>  Py_Initialize();
>>  PyRun_SimpleString("from time import time,ctime\n"
>>                     "print('Today is', ctime(time()))\n");
>>  Py_Finalize();
>>  return 0;
>> }
>> Now, what should I do next? I though I would just "somehow" compile and
>> link Python source code with test.c and that's it but when I do "gcc -I
>> ./Python-3.3.0b1 -o test test.c" I get lots of errors. (Note: I'm total
>> noob what this whole gcc and static linking and .a files goes...)
>> - Next I tried to run "./configure" and "make". It finished without errors
>> and it creates "build/lib.macosx-10.6-x86_64-3.3" folder with lots of *.so
>> (?) files but I'm not sure how to make use of them.
>> If building everything from scratch (the first step above) is not an
>> option, what do I need to build? A "framework"? .dylib? .a? And than link
>> test.o against that?
>> - How does Python from python.org get build? Do they use the same
>> "./configure" and "make" as I can? Do they use any special option?
>> To summarize, I have Python source code and test.c and I would like to
>> have
>> one executable which says "Today is ....". Could someone, please, explain
>> in layman terms, the necessary steps?
>> Thanks you very much in advance!
>> Ecir Hana
>> _______________________________________________
>> capi-sig mailing list
>> capi-sig at python.org
>> http://mail.python.org/mailman/listinfo/capi-sig

More information about the capi-sig mailing list