Quick guess: a binary incompatibility, e.g. how you compile your extension
vs how the libboost_python you link against was built.

Your code looks good to me.

I'd try different compile and link options for building your extension.

Alternatively, you could use cmake, e.g. this worked for me a few months
ago:

https://github.com/rwgk/stuff/tree/master/test_with_boost_python

cmake is pretty good at figuring out matching compile and link options.
(You could just look at what it does and then adopt the same options
elsewhere.)

On Sat, May 11, 2024 at 9:24 AM Robert Applin <
robertapplin.develo...@gmail.com> wrote:

> 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
>
> _______________________________________________
> Cplusplus-sig mailing list
> Cplusplus-sig@python.org
> https://mail.python.org/mailman/listinfo/cplusplus-sig
>
_______________________________________________
Cplusplus-sig mailing list
Cplusplus-sig@python.org
https://mail.python.org/mailman/listinfo/cplusplus-sig

Reply via email to