Embedding Python in C

  • Thread starter Marcin 'Qrczak' Kowalczyk
  • Start date
M

Marcin 'Qrczak' Kowalczyk

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?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top