Building MP as a static library
Building MP as a static library
Did someone build MP as a static library, to be linked with an application?
If so please provide Makefile (hopefully some slight changes). Thanks.
If so please provide Makefile (hopefully some slight changes). Thanks.
Re: Building MP as a static library
Compiling a static library is just a matter of invoking ar. This seems to work, here's the diff:
This produces microptython.a lib after invoking `make staticlib`. Note I renamed main() to mpmain().
Then creat a new source file and build an executable using this static lib:
gcc altmain.c micropython.a -lreadline -ldl -lffi -lm
Code: Select all
diff --git a/py/mkenv.mk b/py/mkenv.mk
index 4bc71ed..24b56e7 100644
--- a/py/mkenv.mk
+++ b/py/mkenv.mk
@@ -45,6 +45,7 @@ SED = sed
PYTHON = python
AS = $(CROSS_COMPILE)as
+AR = $(CROSS_COMPILE)ar
CC = $(CROSS_COMPILE)gcc
LD = $(CROSS_COMPILE)ld
OBJCOPY = $(CROSS_COMPILE)objcopy
diff --git a/py/mkrules.mk b/py/mkrules.mk
index 6127ece..fc30cd9 100644
--- a/py/mkrules.mk
+++ b/py/mkrules.mk
@@ -79,10 +79,16 @@ ifndef DEBUG
endif
$(Q)$(SIZE) $(PROG)
-clean: clean-prog
+staticlib: $(OBJ)
+ $(ECHO) "LIB $(PROG)"
+ $(Q)$(AR) rcs $(PROG).a $(OBJ)
+
+clean: clean-prog clean-staticlib
clean-prog:
$(RM) -f $(PROG)
$(RM) -f $(PROG).map
+clean-staticlib:
+ $(RM) -f $(PROG).a
.PHONY: clean-prog
endif
diff --git a/unix/main.c b/unix/main.c
index d0222de..59a6573 100644
--- a/unix/main.c
+++ b/unix/main.c
@@ -264,7 +264,7 @@ void pre_process_options(int argc, char **argv) {
#define PATHLIST_SEP_CHAR ':'
#endif
-int main(int argc, char **argv) {
+int mpmain(int argc, char **argv) {
mp_stack_set_limit(32768);
pre_process_options(argc, argv);
Then creat a new source file and build an executable using this static lib:
Code: Select all
int main(int argc, char** argv) {
return mpmain(argc, argv);
}
Re: Building MP as a static library
This is great. Thank you.
Re: Building MP as a static library
Hi again,
in order to use the MPY as a library, I would like to know how to use the stack and heap and what to set them to.
Taking some example from the minimal port there is a heap variable being set and passed to gc_init().
With stmhal mp_stack_set_limit() is used to set the stack limit.
I intend to use the MPY as a library within an RTOS task.
Please provide any recommendations on setting the memory stack/heap and what would be a reasonable size.
Thanks.
in order to use the MPY as a library, I would like to know how to use the stack and heap and what to set them to.
Taking some example from the minimal port there is a heap variable being set and passed to gc_init().
With stmhal mp_stack_set_limit() is used to set the stack limit.
I intend to use the MPY as a library within an RTOS task.
Please provide any recommendations on setting the memory stack/heap and what would be a reasonable size.
Thanks.
Re: Building MP as a static library
The reasonable size really depends on your platform (available memory, max stack size of threads if any) and application (i.e. whether you need a lot of memory available or not) so it's hard to tell.
If you compile with MICROPY_ENABLE_GC you have to set both heap size and stack limit. I'd set it to something reasonable, then run your application and use
to show what is actually being used. Then tune the numbers according to that.
If you compile with MICROPY_ENABLE_GC you have to set both heap size and stack limit. I'd set it to something reasonable, then run your application and use
Code: Select all
import micropython
micropython.mem_info()
Re: Building MP as a static library
Ok will give it a try.
Any minimums to set initially?
Any minimums to set initially?
Re: Building MP as a static library
If not too much trouble to ask for Makefile help.
I tried to build the library in bare-arm (make staticlib) with no luck, under unix it builds.
Any info on how to get the bare-arm to build as a library (based on the .mk modifications suggested earlier).
Thanks.
I tried to build the library in bare-arm (make staticlib) with no luck, under unix it builds.
Any info on how to get the bare-arm to build as a library (based on the .mk modifications suggested earlier).
Thanks.
Re: Building MP as a static library
I don't have any target atm I can test bare-arm on, but maybe you can already post the error message(s) you get so at least we know more about what is going on?