How to include existing static library (.a) to application link line?

Hi,
I am unable to add a static library (i.e already compiled for ARM) to my user application. It is part of a proprietary SDK, so only have the header files. I tried specifying LIB_DEP and LIB_DIRS in my custom make file but the library file never gets included in the make print out. I took a look at examples in the firmware (e.g. the firmware/hal/src/photon/lib/FreeRTOS/) where this seems to be done, but without any success. The whole build system is still a bit of a mystery for me…

Thank you

I found a solution which works, albeit not a very elegant one.
You can add you own static library in

/modules/photon/user-part/makefile

similar to the exsiting STM32F2xx_Peripheral_Libraries.a

e.g. I added my mplmpu.a with the following lines:

..
LIB_DEPS += $(PROJECT_ROOT)/user/applications/photon-mpu/inv/mpl/mplmpu.a
LIB_DIRS += $(dir $(LIB_DEPS))
..
LDFLAGS += -Wl,--whole-archive $(PROJECT_ROOT)/hal/src/photon/lib/STM32F2xx_Peripheral_Libraries.a $(PROJECT_ROOT)/user/applications/photon-mpu/inv/mpl/mplmpu.a -Wl,--no-whole-archive
1 Like

@tlangmo, I need to do exactly the same as you did, but it,s not working for me. Did you change anything else?
How do you compile your application? After doing the same change you did, I’m calling:

make APP=myapp PLATFORM=photon

It looks like the library was include, that was extracted from the make print:

...
-Wl,--whole-archive ../../../hal/src/photon/lib/STM32F2xx_Peripheral_Libraries.a
../../../user/applications/myapp/myLibrary/myLibrary.a -Wl,--no-whole-archive 
...
-L../../../user/applications/myapp/myLibrary/
...

The compiler ends with an error saying: "undefined reference to "functionOnMyLibrary()"
The error didn’t change after I made the changes to the make file

/modules/photon/user-part/makefile 

Any idea? Could be a problem with the library and not with the make files?

Thanks

@ScruffR, can you give me some help here?

Not really, this would be something for @jvanier instead, I think.

Hum, I’m not sure how to do that either. For now the best way would be to try other makefile tweaks.

1 Like

It still works the way I described it here. I only ever got it to work with the modular build though, so please make sure export MODULAR=y is set.

The library itself could always be the problem as well. I suggest creating a really simple C library first to try it out:

awesome.c
double awesome_call() {
   return 2.0;
}

awesome.h
#ifndef __AWESOME__
#define __AWESOME__
double awesome_call();
#endif 

to compile, use the same build flag as in build/arm-tools.mk

arm-none-eabi-gcc  -g3 -gdwarf-2 -Os -mcpu=cortex-m3 -mthumb -c awesome.c -o awesome.o
arm-none-eabi-ar  rcs libawesome.a awesome.o

Then in modules/photon/user-part/makefile add your static library (take care of correct path)

LDFLAGS += -Wl,--whole-archive $(PROJECT_ROOT)/hal/src/photon/lib/STM32F2xx_Peripheral_Libraries.a $(PROJECT_ROOT)/../awesome_lib/libawesome.a -Wl,--no-whole-archive

Then in build/module.mk add your include path (or dump the header in any already known path)

INCLUDE_DIRS += $(addsuffix /src,$(MODULE_LIBSV2))
INCLUDE_DIRS += $(PROJECT_ROOT)/../awesome_lib/

Then you can use

extern "C" {
#include "awesome.h"
}

in any file and you are good to go.

Good luck. Should work with c++ as well.

2 Likes

@tlangmo, great tutorial! Thanks a lot for explaining all the steps! I’m missing to include the (extern “C”) call, probably the reason it still not working. I’ll sure it will work now.

Before you reply, I got it compiling right, but a hard fault happens every time a library function is called.
An additional information is that, it’s not compiling using the repository on it’s system 0.6.0. When I changed to system 0.5.3 it compiled right, but with the problem I described before.

I’m already using a test library like you said, it was created exacly like your example. I’ll follow up after I give one more try. Thanks again.

@bacichetti. Thanks for trying. Sorry, the hard-fault is caused because the compiler flags did not match. If you use the ones from build/arm-tools.mk, it works. I have edited my original post. Btw, I always use only the develop branch.

@tlangmo, your instructions worked perfectly, thanks!!