C++ module New/Delete and math and C libraries
Posted: Tue May 24, 2022 6:35 pm
I have a C++ code that I can compile as Cpp module following the upy 1.18 documentation, but Im not sure how to "link" New and Delete operators with micropython garbage collector.
Im wondering if this is the correct way:
Also my C++ code is a precompiled library that depends on math library, but I have some problems cause G++ cant link with the sin/cos functions offered by micropython.
My micropython.mk is:
I think -lm is duplicated cause micropython uses its own implementation, and I think -lc too.
Can someone with more experience guide me?
Im wondering if this is the correct way:
Code: Select all
void *operator new(size_t size) {
return gc_alloc(size, 0);
}
void *operator new[](size_t size) {
return gc_alloc(size, 0);
}
void operator delete(void *p) {
gc_free(p);
}
void operator delete[](void *p) {
gc_free(p);
}
My micropython.mk is:
Code: Select all
CPPEXAMPLE_MOD_DIR := $(USERMOD_DIR)
# Add our source files to the respective variables.
SRC_USERMOD += $(CPPEXAMPLE_MOD_DIR)/ppl_module.c << here are the mp_rom_map_elem_t ppl_module_globals_table
SRC_USERMOD += $(CPPEXAMPLE_MOD_DIR)/sysmem.c << here I needed to reimplement syscall _sbrk. I think I can solve with new/delete overloading and redirecting to upy gc.
SRC_USERMOD += $(CPPEXAMPLE_MOD_DIR)/syscalls.c << here I reimplemented mocks of _read, _write, _lseek...needed by -lc
SRC_USERMOD_CXX += $(CPPEXAMPLE_MOD_DIR)/ppl.cpp << here are the upy<>cpp wrappers
# Add our module directory to the include path.
CFLAGS_USERMOD += -I$(CPPEXAMPLE_MOD_DIR)
CXXFLAGS_USERMOD += -I$(CPPEXAMPLE_MOD_DIR) -std=c++11
LDFLAGS_USERMOD += -lpointperfect - lstdc++ -lc -lm -L$(USERMOD_DIR)
Can someone with more experience guide me?