2

I normally use Python but I now want to learn a bit about interfacing c++ with Python. For this I wrote a simple program in c++:

#include <iostream>
using namespace std;

int SomeCalculation(float x){
    int decision = 0;
    if (x > 1){
        decision = 1;
    }
    return decision;
}

int main()
{
    float a = 0.5;
    cout << "\n" << SomeCalculation(a) << "\n\n";
    return 0;
}

Using CodeBlocks I compiled it and it runs fine. I now want to import and use SomeCalculation() into Python. As far as I understand (from this) I need to compile the cpp program into a shared library to be imported in Python. I found this extensive SO thread about that, but I'm totally lost in it.

I've got a main.cpp file (the code above) which I need to compile into an .so file (right?). I tried the following: g++ -fPIC -g -ggdb -c main.cpp -o main.so. I then try to import the resulting .so file into my python program as follows:

import ctypes
print ctypes.CDLL('main.so').SomeCalculation(2)

But I get the following error:

Traceback (most recent call last):
  File "/home/kram/c++/cmod/importcpp.py", line 2, in <module>
    print ctypes.CDLL('main.so').SomeCalculation(2)
  File "/usr/lib/python2.7/ctypes/__init__.py", line 365, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: main.so: cannot open shared object file: No such file or directory

Since I've never really (manually) compiled a c++ program I'm kind of lost in the command to do so. Does anybody have a tip on how to compile this as a shared library? All tips are welcome!

Community
  • 1
  • 1
kramer65
  • 50,427
  • 120
  • 308
  • 488

1 Answers1

4

Your .so is fine, as far as it goes. However, Python isn't finding it:

OSError: main.so: cannot open shared object file: No such file or directory


If you need to import a C or C++ module without changing it, you can use SWIG to generate a wrapper, or do it manually using the Python or Boost.Python mechanisms mentioned below.


If you were writing a Python extension from scratch, you'd find it expects some hooks to initialize your module, figure out what functions it exports, etc.

See the documentation for details - there are macros defined to help you write the hooks Python needs.

It may be easier in practise to use Boost.Python instead, but doing it directly should be ok for a simple example.

Useless
  • 64,155
  • 6
  • 88
  • 132
  • Thanks for the suggestions. The things is that I want to be able to import this c++ program without the need to modify anything in the c++ program. The reason for this is that I will need to import another c++ program with some decision logic which I am prohibited to see. I do not want to impose any extra coding to the owners of the c++ program. Is this possible with Boost.Python or would I need to use something else for that? – kramer65 Jun 03 '13 at 16:30
  • You could write a wrapper directly with Boost.Python or by hand, or just use SWIG to generate it. – Useless Jun 03 '13 at 16:34
  • 2
    Instead of adding the directory to `$PYTHONPATH` or `sys.path`, you can also just pass the absolute path of the shared library to `ctypes.CDLL()`. Also, beware of [name mangling](http://en.wikipedia.org/wiki/Name_mangling#Name_mangling_in_C.2B.2B) -- you're going to need to either add `extern "C"` around the exported C++ function's definition (recommended), or import it by its mangled name, e.g. `ctypes.CDLL('main.so').__Z15SomeCalculationf`. – Adam Rosenfield Jun 03 '13 at 16:44
  • That isn't the same error as your question - can you edit in the code you tried and the error? – Useless Jun 04 '13 at 11:01
  • Excuse me. That was for another SO discussion.. :S :) – kramer65 Jun 04 '13 at 12:40