3

I have written simple HelloWorld class. and write a boost python wrapper.and debug the code as DLL.My question is how can i expose this code in python and use greet function.I tried by giving the path in sys.path.insert. but not able to get greet function. The code i treid is below. Thanks for help.

#include<boost/python.hpp>

using namespace std;
using namespace boost::python;
class World
{
 public:
 string msg;
 void set(string msg)
{
    this->msg=msg;
}

 string greet()
 {
   return msg;
 }

};

BOOST_PYTHON_MODULE(ExpsoingClasses)
{
class_<World>("World")
    .def("greet", &World::greet)
    .def("set", &World::set)

    ;
}
4
  • Possible duplicate: stackoverflow.com/questions/145270/calling-c-c-from-python Commented Sep 4, 2013 at 9:36
  • I donot know how can i call it from Python for exposing its greet funtion. Commented Sep 4, 2013 at 9:40
  • Does this compile? What does your python code look like? You are loading your module in Python? Commented Sep 4, 2013 at 15:02
  • @user1628622 yes its compiled but i am not able to get greet and set function in python. only ExpsoingClasses module is importted. Commented Sep 5, 2013 at 9:04

1 Answer 1

3

At least on my system, I had to rename the library file from ExpsoingClasses.dll to ExpsoingClasses.pyd before I could import it in Python. Once you do that, this should work:

import ExpsoingClasses
retVal = ExpsoingClasses.World()
retVal.set('hello world')
print retVal.greet()
Sign up to request clarification or add additional context in comments.

9 Comments

thanks for the help. I build as a debug version and which path of file i have to give in sys.path,insert. I wrote sys.path.insert(0,"C:\Documents and Settings\X157574\Desktop\Kashif\Boost Python Examples\Exposing Classes\debug"). but its not working. and i also tried rename the dll to .pyd but its not working
I don't think Python likes paths in that format. You want pairs of forward slashes rather than backslashes. Try this: sys.path.insert(0,"C://Documents and Settings//X157574//Desktop//Kashif//Boost Python Examples//Exposing Classes//debug") Also, you can seperate the issue of "is my .pyd functional" from the issue of "can python find my .pyd" by copying the .pyd to the same folder as the python script you're writing.
thanks for the help. now i am able to import ExposingClasses. but still its greet and set function is not accessable
The only thing that comes to mind is the spelling of "ExposingClasses" in your code; it says "ExpsoingClasses". Make sure you use the same spelling everywhere.
Thanks. I Correct the mistake.but still i cannot access World Class and set and greet fucntions.I am using python 3.Is there any other way to access these method?
|

Your Answer

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.