summaryrefslogtreecommitdiffstatshomepage
path: root/examples/usercmodule/cppexample
diff options
context:
space:
mode:
authorstijn <stijn@ignitron.net>2020-10-21 11:13:47 +0200
committerDamien George <damien@micropython.org>2020-10-29 15:30:42 +1100
commit25c4563f26c2270cde01b01dca0e8b801c9c8282 (patch)
tree893f7160c558bceb288f06642d2fae440d51014d /examples/usercmodule/cppexample
parentfad4079778f46bc21dd19a674b31b4c3c7eb6a91 (diff)
downloadmicropython-25c4563f26c2270cde01b01dca0e8b801c9c8282.tar.gz
micropython-25c4563f26c2270cde01b01dca0e8b801c9c8282.zip
examples: Add example code for user C modules, both C and C++.
Add working example code to provide a starting point for users with files that they can just copy, and include the modules in the coverage test to verify the complete user C module build functionality. The cexample module uses the code originally found in cmodules.rst, which has been updated to reflect this and partially rewritten with more complete information.
Diffstat (limited to 'examples/usercmodule/cppexample')
-rw-r--r--examples/usercmodule/cppexample/example.cpp17
-rw-r--r--examples/usercmodule/cppexample/examplemodule.c25
-rw-r--r--examples/usercmodule/cppexample/examplemodule.h5
-rw-r--r--examples/usercmodule/cppexample/micropython.mk12
4 files changed, 59 insertions, 0 deletions
diff --git a/examples/usercmodule/cppexample/example.cpp b/examples/usercmodule/cppexample/example.cpp
new file mode 100644
index 0000000000..06809732a4
--- /dev/null
+++ b/examples/usercmodule/cppexample/example.cpp
@@ -0,0 +1,17 @@
+extern "C" {
+#include <examplemodule.h>
+
+// Here we implement the function using C++ code, but since it's
+// declaration has to be compatible with C everything goes in extern "C" scope.
+mp_obj_t cppfunc(mp_obj_t a_obj, mp_obj_t b_obj) {
+ // Prove we have (at least) C++11 features.
+ const auto a = mp_obj_get_int(a_obj);
+ const auto b = mp_obj_get_int(b_obj);
+ const auto sum = [&]() {
+ return mp_obj_new_int(a + b);
+ } ();
+ // Prove we're being scanned for QSTRs.
+ mp_obj_t tup[] = {sum, MP_ROM_QSTR(MP_QSTR_hellocpp)};
+ return mp_obj_new_tuple(2, tup);
+}
+}
diff --git a/examples/usercmodule/cppexample/examplemodule.c b/examples/usercmodule/cppexample/examplemodule.c
new file mode 100644
index 0000000000..ceb588bef6
--- /dev/null
+++ b/examples/usercmodule/cppexample/examplemodule.c
@@ -0,0 +1,25 @@
+#include <examplemodule.h>
+
+// Define a Python reference to the function we'll make available.
+// See example.cpp for the definition.
+STATIC MP_DEFINE_CONST_FUN_OBJ_2(cppfunc_obj, cppfunc);
+
+// Define all properties of the module.
+// Table entries are key/value pairs of the attribute name (a string)
+// and the MicroPython object reference.
+// All identifiers and strings are written as MP_QSTR_xxx and will be
+// optimized to word-sized integers by the build system (interned strings).
+STATIC const mp_rom_map_elem_t cppexample_module_globals_table[] = {
+ { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_cppexample) },
+ { MP_ROM_QSTR(MP_QSTR_cppfunc), MP_ROM_PTR(&cppfunc_obj) },
+};
+STATIC MP_DEFINE_CONST_DICT(cppexample_module_globals, cppexample_module_globals_table);
+
+// Define module object.
+const mp_obj_module_t cppexample_user_cmodule = {
+ .base = { &mp_type_module },
+ .globals = (mp_obj_dict_t *)&cppexample_module_globals,
+};
+
+// Register the module to make it available in Python.
+MP_REGISTER_MODULE(MP_QSTR_cppexample, cppexample_user_cmodule, MODULE_CPPEXAMPLE_ENABLED);
diff --git a/examples/usercmodule/cppexample/examplemodule.h b/examples/usercmodule/cppexample/examplemodule.h
new file mode 100644
index 0000000000..d89384a630
--- /dev/null
+++ b/examples/usercmodule/cppexample/examplemodule.h
@@ -0,0 +1,5 @@
+// Include MicroPython API.
+#include "py/runtime.h"
+
+// Declare the function we'll make available in Python as cppexample.cppfunc().
+extern mp_obj_t cppfunc(mp_obj_t a_obj, mp_obj_t b_obj);
diff --git a/examples/usercmodule/cppexample/micropython.mk b/examples/usercmodule/cppexample/micropython.mk
new file mode 100644
index 0000000000..e10d965a00
--- /dev/null
+++ b/examples/usercmodule/cppexample/micropython.mk
@@ -0,0 +1,12 @@
+CPPEXAMPLE_MOD_DIR := $(USERMOD_DIR)
+
+# Add our source files to the respective variables.
+SRC_USERMOD += $(CPPEXAMPLE_MOD_DIR)/examplemodule.c
+SRC_USERMOD_CXX += $(CPPEXAMPLE_MOD_DIR)/example.cpp
+
+# Add our module directory to the include path.
+CFLAGS_USERMOD += -I$(CPPEXAMPLE_MOD_DIR)
+CXXFLAGS_USERMOD += -I$(CPPEXAMPLE_MOD_DIR)
+
+# We use C++ features so have to link against the standard library.
+LDFLAGS_USERMOD += -lstdc++