32

I'm experimenting with python functions right now. I've found a way to import python functions into c/c++ code, but not the other way around.

I have a c++ program written and it has a certain function in it. I'd like to "import" the compiled c++ program into my python script and call the c++ function.

For simplicity, say the c++ function is as simple as:

int square(x)
{
  return x*x;
}

and the compiled program is named Cprog.

I'd like my python script to be something like:

import Cprog

print Cprog.square(4)

Is this possible? I've searched the internet to no avail and I'm hoping one of you gurus might have a clever way of going about this...

Nick
  • 329
  • 1
  • 3
  • 4
  • 2
    Have you tried Boost::Python? – robbrit Nov 22 '10 at 01:02
  • No I haven't. But it looks like it might do what I want. I'm looking into it at the moment. Thanks a lot! Would anybody know of a less complicated way of doing this though (without the use of a third-wheel module)? – Nick Nov 22 '10 at 01:12
  • Possible duplicate of [Calling C/C++ from Python?](https://stackoverflow.com/questions/145270/calling-c-c-from-python) – Trevor Boyd Smith Apr 25 '19 at 11:44

5 Answers5

29

Here is a little working completion of the simple example above. Although the thread is old, I think it is helpful to have a simple all-embracing guide for beginners, because I also had some problems before.

function.cpp content (extern "C" used so that ctypes module can handle the function):

extern "C" int square(int x)
{
  return x*x;
}

wrapper.py content:

import ctypes
print(ctypes.windll.library.square(4)) # windows
print(ctypes.CDLL('./library.so').square(4)) # linux or when mingw used on windows

Then compile the function.cpp file (by using mingw for example):

g++ -shared -c -fPIC function.cpp -o function.o

Then create the shared object library with the following command (note: not everywhere are blanks):

g++ -shared -Wl,-soname,library.so -o library.so function.o

Then run the wrapper.py an the program should work.

Alan
  • 1,582
  • 1
  • 13
  • 11
dolby
  • 643
  • 1
  • 8
  • 10
  • Tried this and got (WindowsError(126, 'The specified module could not be found'), ) for the first one and ; (WindowsError(193, '%1 is not a valid Win32 application'), ) for the second one. Any help? – azazelspeaks Aug 08 '18 at 21:52
10

If you build your program as a shared library/DLL, you could use ctypes to call it.

import ctypes
print ctypes.windll.cprog.square(4) # windows
print ctypes.CDLL('cprog.so').square(4) # linux
kichik
  • 33,220
  • 7
  • 94
  • 114
  • I forgot about ctypes, definately the way to go if its just a couple of functions you want exposed. – DaedalusFall Nov 22 '10 at 01:20
  • What's the best way to create a *.so file? Sorry, I'm not a computer programmer/scientist, and my g++ command is usually along the lines of "g++ Ccode.cpp -o Cfile". I've tried using -shared as an additional option along with the linux line suggested above and I received an error. I don't think I've created my *.so file correctly...any advice? – Nick Nov 22 '10 at 01:34
  • @Nick: That's a different question, one that has already been asked: http://stackoverflow.com/questions/3588476/building-a-shared-library-using-gcc – SingleNegationElimination Nov 22 '10 at 02:55
5

You need to create a python module with that function in it. There are three main ways:

  1. Using Swig - this reads your c code and creates a python module from it.
  2. Hand coded using the python c api.
  3. Using Boost::Python (often the easiest way).

This pdf covers 1 and 2. This page will tell you how to use Boost::Python.

You cannot (easily) use a function that is in a c/c++ program - it must be in a static library (which you can also link your c/c++ program against).

EDIT - Cython Is also worth a mention.

DaedalusFall
  • 8,335
  • 6
  • 30
  • 43
4

You want to extend python with a C/C++ module. The following Python documentation is a good place to start reading: http://docs.python.org/extending/extending.html

Anthony
  • 12,177
  • 9
  • 69
  • 105
0

There are a lot of different ways to wrap C++ code to be used in Python. Most are listed on the Python wiki here.

I've found a decently easy way to automate it is to use py++ to automatically generate the wrappers, then compile the generated files. But this is more appropriate for larger projects where wrapping everything by hand is just not very feasible and would cause a maintenence nightmare.

Jonathan Sternberg
  • 6,421
  • 7
  • 39
  • 58