Embedding Python in C

Discussion in 'Python' started by Marcin 'Qrczak' Kowalczyk, Jun 9, 2004.

  1. I made a bridge between my language Kogut
    <http://qrnik.knm.org.pl/~qrczak/kogut/kogut.html> and Python,
    which gives my language instant access to Python libraries.
    (I'm only linking to libpython, I'm not using the standalone
    interpreter.)

    I have a question about the proper way of compilation and linking.
    Currently I do it like this:

    I assume that the interpreter installed as 'python' determines the version.
    I check Python version:

    if python -c 'import sys; sys.exit(not hasattr(sys, "version_info"))'; then
    ko_cv_python_major=`python -c 'import sys; print sys.version_info[0]'`
    else
    ko_cv_python_major=ancient
    fi

    and similarly the minor version. Currently I accept only 2.2 and higher
    because of language features (iterators); while I could support earlier
    versions too with appropriate conditional compilation, I didn't bother.

    I check Python include directory:

    ko_cv_python_include=`python -c 'from distutils import sysconfig
    print sysconfig.get_python_inc()'`

    I add this directory to -I options of the C compiler (currently GCC only
    is supported), and #include <Python.h>.

    For linking, I add
    -lpython
    for version 2.3 and above, and
    -lpython -ldl -lpthread -lutil -lm
    for earlier versions, i.e. 2.2 only.

    Problems:

    1. How to do it better? For example I feel uneasy about the way I specify
    which libraries should be linked. Is there a way to determine it in a
    more robust way?

    2. Someone has reported to me that he has no distutils installed. Why
    could it be? I thought distutils is a part of the core Python package.
    Can I rely on distutils, and if not, how to determine the include
    directory?

    3. I assume that -lpython is the same version as the 'python' program,
    and give no way to select alternative versions if multiple versions
    are installed. Should I do it in some better way?

    --
    __("< Marcin Kowalczyk
    \__/
    ^^ http://qrnik.knm.org.pl/~qrczak/
     
    Marcin 'Qrczak' Kowalczyk, Jun 9, 2004
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Darryl

    Embedding Python in Python

    Darryl, Oct 8, 2003, in forum: Python
    Replies:
    6
    Views:
    423
    Darryl
    Oct 14, 2003
  2. Phil Frost

    Embedding Python in Python

    Phil Frost, Aug 18, 2004, in forum: Python
    Replies:
    23
    Views:
    788
    Paul Rubin
    Aug 19, 2004
  3. Maurice LING

    embedding python in python

    Maurice LING, Sep 29, 2004, in forum: Python
    Replies:
    8
    Views:
    403
    Jeff Shannon
    Oct 1, 2004
  4. Kakacek
    Replies:
    1
    Views:
    392
    Kakacek
    Jan 12, 2006
  5. abhinav
    Replies:
    1
    Views:
    452
    Armin Steinhoff
    Feb 19, 2006
Loading...

Share This Page