0

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.

asked Apr 11, 2012 at 6:12
1
  • Who thinks this is "not a real question"? Why? Commented Apr 11, 2012 at 7:51

4 Answers 4

2

Check out Boost::Python.

answered Apr 11, 2012 at 6:19
Sign up to request clarification or add additional context in comments.

2 Comments

thank you friend.. but I am not supposed to write wraaper functions which i will need to write in boost Python..
"supposed to"? Isn't it your project?
1

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.

answered Apr 11, 2012 at 6:26

4 Comments

You mean to say i first need to study writing python extension module(I am not supposed to use wrapper functions) to accomplish my task..
Your question says nothing about these extra constraints you are adding in comments. Please can you correct the question.
I have edited my question to include my constraint. Now, can you advice me on how to move forward.
Your options are 1. Wrap the C++ code in a C style wrapper. 2. Write an extension module. SWIG and Boost.Python both make it easier for you to create an extension module.
0

An alternative to Boost.Python is SWIG.

answered Apr 11, 2012 at 6:20

Comments

0

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.

answered Apr 11, 2012 at 6:24

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.