I need to create Python bindings for a C++ library and after a brief survey of the available options was planning on using SWIG ( http://www.swig.org/tutorial.html ). How does this compare?
Cppyy – Automatic Python-C++ bindings
11–20 of 25 posts
Re: Cppyy – Automatic Python-C++ bindings
#12Does anyone know of any well known tools that generate bindings in other languages e.g., Python from C++ code? IIUC, this tool does the opposite i.e., generate C++/other language bindings from Python
Re: Cppyy – Automatic Python-C++ bindings
#13Even so this will be awesome for unit testing. I really like the testing interface pytest provides, along with its parametrized tests.
Anyone else using python to test the logic their C/C++ code? I can't think of how I would test the memory allocation and freeing portion. Anyone think it's a bad idea?
Re: Cppyy – Automatic Python-C++ bindings
#14Re: Cppyy – Automatic Python-C++ bindings
#15 const char *cls::func(void)
{
return "abc"; // static: caller must not free
}
and const char *cls::func(void)
{
return strdup("abc"); // caller must free
}
Edit: I see: designed for large scale programs in high performance computing that use modern C++.Modern C++ meaning, no pointers anywhere.
Re: Cppyy – Automatic Python-C++ bindings
#16This is super interesting but if I am looking to bring C++ into my Python code base it would be because performance is important enough to do so. With that in mind I am not sure how this stacks up against compiled half steps like Cython or foreign function interfaces likes ctypes or CFFI instead.
[1] https://www.benfrederickson.com/writing-python-extensions-in...
Re: Cppyy – Automatic Python-C++ bindings
#17From the comments below, it sounds like interfacing my C++ code with python using Cppyy would sacrifice performance. Even so this will be awesome for unit testing. I really like the testing interface pytest provides, along with its parametrized tests. Anyone else using python to test the logic their C/C++ code? I can't think of how I would test the memory allocation and freeing portion. Anyone think it's a bad idea?
But one can test the interface to the program nicely from python (i.e. text or socket i/o).
Re: Cppyy – Automatic Python-C++ bindings
#18How does this know the difference between: const char *cls::func(void) { return "abc"; // static: caller must not free } and const char *cls::func(void) { return strdup("abc"); // caller must free } Edit: I see: designed for large scale programs in high performance computing that use modern C++. Modern C++ meaning, no pointers anywhere.
So you handle it like any other ctypes pointer, ie. `libc = ctypes.CDLL(find_library("c")); libc.free(ptr)`
Which is not fun in Python. So yeah, better avoid raw pointers.
Re: Cppyy – Automatic Python-C++ bindings
#19How does this compare to pybind11?
If you have a C++ project, you want to have Python bindings, and you don't want to share your header files, pybind11 is the way to go. If you want to / can share your header files, cppyy is a very convenient way of being able to call your C++ code from Python. I'm not sure if there is a way to use it without headers.
Re: Cppyy – Automatic Python-C++ bindings
#20Earlier quoted context omitted.
If you have a C++ project, you want to have Python bindings, and you don't want to share your header files, pybind11 is the way to go. If you want to / can share your header files, cppyy is a very convenient way of being able to call your C++ code from Python. I'm not sure if there is a way to use it without headers.
How do you link any cpp binary without headers without guessing the calling convention?
I looked into it a bit further, and cppyy does have provisions for this: https://cppyy.readthedocs.io/en/latest/bindings_generation.h...
I may have misread that page, but it looks like you can create a .rootmap and .pcm file and distribute that instead of the C++ headers.