11

I'm working in an embedded Linux environment and I have some Python code which I would like to use. My Python code is just doing some math, not using any library other than Numpy and the common ones.

Is there any way to build up a library that I can call from C or C++ code?

stef
  • 737
  • 4
  • 13
  • 31
  • 4
    http://docs.python.org/extending/embedding.html may be of interest. – FatalError Feb 02 '12 at 18:39
  • Also consider to actually make a C/C++ library to use in Python code (= extending as opposed to embedding). It's easy to overlook that this scenario can actually be more interesting for many applications – Kos Feb 02 '12 at 18:41
  • 1
    Check out [Boost.Python](http://www.boost.org/doc/libs/1_48_0/libs/python/doc/)! – Ferdinand Beyer Feb 02 '12 at 18:41
  • 1
    Duplicate or at least interesting for you: http://stackoverflow.com/questions/4331599/calling-python-functions-from-c – Johan Lundberg Feb 02 '12 at 18:45

1 Answers1

15

Embedding the CPython interpreter into a C or C++ program is actually pretty straightforward.

The official documentation has some complete examples.

Also, check out SWIG and Boost.Python.

NPE
  • 486,780
  • 108
  • 951
  • 1,012