-
I've been using someone else's code as a c++ user module for a while, following the example at examples/usercmodule/cppexample However, the maintainer of the original code has recently started using c++ vector Since the user module declaration has to be compatible with C everything goes in extern "C" scope, and declarations like Am I correct in understanding that a "C++ User module" can use C++ syntax in the source file, but we are limited to "C compatible" in function declarations etc? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
You are correct (if I understand you correctly). C++ code can use C++ functions/methods internally; it's just that the functions that are called by Python code must be callable from C (no name mangling, etc.). You can ensure this by declaring them as You could use a |
Beta Was this translation helpful? Give feedback.
-
I found some example code of a binding for c++ here. Didn't test, if it works, but I could at least build a module with that structure. Basically the module registration happens in c, while the User Module is in c++ (locals). For this, the stand Makros Is this a good practice? |
Beta Was this translation helpful? Give feedback.
You are correct (if I understand you correctly). C++ code can use C++ functions/methods internally; it's just that the functions that are called by Python code must be callable from C (no name mangling, etc.). You can ensure this by declaring them as
extern "C"
(with#ifdef __cplusplus
guards). You also need to be careful with memory allocation, C++ exceptions and static guards, etc.You could use a
std::vector
inside your C++ code, but if you wanted to provide it to MicroPython code it'd have to be presented as abytes
orarray.array
object or similar. You could usestd::vector::cbegin()
to get a pointer to the start of the allocated memory.