Sudip
Sudip

Reputation: 583

How do I call a function defined in C++ object from a python program?

I have a C++ program:

class X
{
    private:
        int a;
    public:
        int func(char *str);
};

Now I want to call the function func in my Python program. How can I do it?

I have successfully called C functions from my Python program using ctypes. However I can't seem to figure out how to call functions defined inside C++ objects.

I have already worked with ctypes, so I would like to figure out how to accomplishing it that way. However I'm open to other techniques. Also my project puts a constarint that I am not supposed to use wrapper function in my C++ program.

Upvotes: 0

Views: 164

Answers (4)

David Heffernan
David Heffernan

Reputation: 612794

You tagged the question ctypes, but you cannot instantiate a C++ object directly from ctypes. You can wrap the C++ object in a C interface and then use ctypes. Or you could write a Python extension module which could include C++ code.

Upvotes: 1

xgwang
xgwang

Reputation: 646

try http://swig.org/ years ago i wrapped some c/c++ code to use them in wxPython with swig. i cannot remember too much details but i think swig is easy to use.

Hope that helps.

Upvotes: 0

HighCommander4
HighCommander4

Reputation: 52739

An alternative to Boost.Python is SWIG.

Upvotes: 0

Karl Knechtel
Karl Knechtel

Reputation: 61479

Check out Boost::Python.

Upvotes: 2

Related Questions