Some time ago I needed to link with ARM DSP library on NuttX to use FFT feature and I decided to document how I did it and some tricks to reduce the final binary size.
In fact to include a library all you need to do is add it to your board Make.defs. See a simple patch file to stm32f4discovery:
index 59aa60bf6b..5e6845c81c 100644 --- a/boards/arm/stm32/stm32f4discovery/scripts/Make.defs +++ b/boards/arm/stm32/stm32f4discovery/scripts/Make.defs @@ -63,6 +63,8 @@ CXXPICFLAGS = $(ARCHPICFLAGS) $(CXXFLAGS) CPPFLAGS = $(ARCHINCLUDES) $(ARCHDEFINES) $(EXTRAFLAGS) AFLAGS = $(CFLAGS) -D__ASSEMBLY__ +EXTRA_LIBS = "$(TOPDIR)/3rparty/libarmdsp.a" + NXFLATLDFLAGS1 = -r -d -warn-common NXFLATLDFLAGS2 = $(NXFLATLDFLAGS1) -T$(TOPDIR)/binfmt/libnxflat/gnu-nxflat-pcrel.ld -no-check-sections LDNXFLATFLAGS = -e main -s 2048
It means you need to create a “3rdpary” directory at root of nuttx/ and put your library there.
But after the compilation you will notice that your nuttx.bin binary will become very big. It happens because the linker will include all the functions from the library.
You can instruct it to include only the needed functions using this parameter with LDFLAGS:
LDFLAGS += --gc-sections
Now your binary will become way smaller
10 thoughts on “Linking external libraries on NuttX”
Do you mean ARM CMSIS DSP library? If so, this is great news; we can include NN library as well, and these together simplify many AI applications including industrial predictive meintenance.
Could you please expand this blog a little more (for us non-expert readers! ); such as how the directory structure should be, how the functions should be called etc.
Yes, it was the ARM CMSIS DSP library, I will include more details to let you and other people use it. But basically it is just a matter of following the trick and gave and using “git grep EXTRA_LIBS” to find the right place! 😉
I did these steps, but I only get the feedback:
arm-none-eabi-ld: lib_strtof.c :(. text + 0xa6): undefined reference to `__aeabi_fadd ‘
arm-none-eabi-ld: lib_strtof.c :(. text + 0xb2): undefined reference to `__aeabi_fmul ‘
arm-none-eabi-ld: lib_strtof.c :(. text + 0xbe): undefined reference to `__aeabi_i2f ‘
arm-none-eabi-ld: lib_strtof.c :(. text + 0xc4): undefined reference to `__aeabi_fadd ‘
arm-none-eabi-ld: lib_strtof.c :(. text + 0x15c): undefined reference to `__aeabi_fcmplt ‘
arm-none-eabi-ld: lib_strtof.c :(. text + 0x168): undefined reference to `__aeabi_fcmpge ‘
arm-none-eabi-ld: lib_strtof.c :(. text + 0x18a): undefined reference to `__aeabi_fdiv ‘
arm-none-eabi-ld: lib_strtof.c :(. text + 0x194): undefined reference to `__aeabi_fmul ‘
arm-none-eabi-ld: lib_strtof.c :(. text + 0x1a0): undefined reference to `__aeabi_fmul ‘
Do you know how to solve this?
I am working with a Tiva and I’m a newbie with Nuttx. I was trying to add a lib.a to understand how it does it.
Are you using a MCU with FPU support? If so, please try to enable it:
System Type -> [*] FPU support
Good to see an activity in this thread 🙂
Thank you for your help
Yes, I’m using tm4c1294xl, and I found this option yesterday. It solved some errors. To resolve the other errors, I added the “+” in the lib assignment.
EXTRA_LIBPATHS += “$(TOPDIR)/path_to_lib/”
EXTRA_LIBS += -ldriver
Thank you Alan, your guides has been very helpful to me.
You are welcome! Nice to know this post was helpful for you!
I am glad to see that you could resolve the errors.
Could you explain shortly for me and others; what did you do and how you did? which library files did you put in which folders (directory structure), which files did you modify? Any additional info would be also helpful.
I believe this subject is very important for Nuttx !
Alan said he will include more details to let other people use it but I know he is very busy, maybe you can contribute a little.
Of course. But I am trying to understand the concepts of NuttX yet.
I had an application that was in the apps directory, and I needed to use a static lib. So from documentation and guides, I found the following way to do this.
I put the library in the “/boards/arm/tiva/tm4c1294-launchpad/include/” directory
And then, I added the following lines to Make.defs in “$(TOPDIR)/arch/arm/src/tiva/Make.defs”:
EXTRA_LIBPATHS += -L “$(TOPDIR)/boards/arm/tiva/tm4c1294-launchpad/include/lib_folder/”
EXTRA_LIBS += -ldriver
This can also be summarized in
EXTRA_LIBS += “$(TOPDIR)/boards/arm/tiva/tm4c1294-launchpad/include/lib_folder/libdriver.a”
Then, after running make menuconfig, I selected the FPU support option, as indicated by Alan earlier.
After make, nuttx.bin was successfully created.
I found this information in this tutorial and also in this documentation https://cwiki.apache.org/confluence/display/NUTTX/Building+NuttX+with+Applications+Outside+of+the+Source+Tree section 4 Contain the apps/Directory – item 2 Use the EXTRA_LIBS Feature.
Thank you very much Fernando, it is quite clear to me now. Best regards, Murat.