[C++-sig] Boost Python help

Robert Applin robertapplin.developer at gmail.com
Sat May 11 07:58:07 EDT 2024


Hi,

I'm having problems exposing a C++ class to python using Boost::Python
(v1.84). I have defined the following code:

class TestClass {
public:
    TestClass() {}
};

void test(TestClass& self, std::string const &testStr) {
  std::cout << "BEFORE" <<std::endl;
  std::cout << testStr <<std::endl;
  std::cout << "AFTER" <<std::endl;
  // ... use self ...
}

BOOST_PYTHON_MODULE(my_module) {
  using namespace boost::python;

  class_<TestClass, std::shared_ptr<TestClass>>("TestClass").def("test",
&test);

}

I then run the following python script:


from my_module import TestClass

message = "Hello"

class_obj = TestClass()
class_obj.test(message)

but I get the following output:

BEFORE
Segmentation fault

What am I doing wrong?

Many thanks in advance,
Rob
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.python.org/pipermail/cplusplus-sig/attachments/20240511/570099c0/attachment.html>


More information about the Cplusplus-sig mailing list