How to add a pre-compiled library?

Status
Not open for further replies.
Hello,

I need to add a precompiled library (.a file) to a project. How can I accomplish that?
The library is pre-compiled for the cortex M4.

I'm using a Teensy 3.2

Regards
 
I did this once and it's not pretty. Given your lib libtst.a

cp libtst.a arduino-1.8.4/hardware/tools/arm/arm-none-eabi/lib/

edit hardware/teensy/avr/boards.txt for each architecture

teensy35.build.flags.libs=-larm_cortexM4lf_math -lm -ltst

of course, you have to re-do this every time you upgrade IDE. maybe there is a better way
 
I recall seeing mention that Arduino added support for libraries with a pre-compiled .a file into newer versions. I haven't used it and I can't easily find any documentation on how it works, so maybe not?
 
I recall seeing mention that Arduino added support for libraries with a pre-compiled .a file into newer versions. I haven't used it and I can't easily find any documentation on how it works, so maybe not?

there was discussion here https://github.com/arduino/Arduino/...y-specification#libraryproperties-file-format

but I haven't confirmed it works ...

someone tried: https://github.com/arduino/Arduino/issues/7005

EDIT: I don't think the precompiled lib stuff works. I tried with the T3.5/3.6 CryptoAccel library. I edited library.properties and created src/cortex-m4/libcau.a, and built my crypto benchmark sketch (verbose) with 1.8.4. I get undefined references, and the link command shows no references to libcau. Possibly, teensyduino might have to be modified to support the precompiled options ??
For now, use the method described in post #2
 
Last edited:
Hello and thank you for your replys. I got it working in the meantime. :)
I copied the .a file to
<Arduino_Install_Dir>\hardware\tools\arm\arm-none-eabi\lib

Then I edited the boards.txt file found here:
<Arduino_Install_Dir>\hardware\teensy\avr\boards.txt

The library to add be used must be edited before the -lm option
Example
Before:
teensy31.build.flags.libs=-larm_cortexM4l_math -lm
After:
teensy31.build.flags.libs=-larm_cortexM4l_math -lmylib -lm

The sketch compiles and links and I successfully called a library function with a correct output.

Of course this solution is suboptimal since from now on mylib is linked into every teensy31/32 project.
 
Here's my understanding of how the static libraries work. Please correct me if I'm wrong.

Static library files (.a) are 'archives' of the object files that were compiled (.o). You can think of a .a file as tarball of the .o files. When the linker uses this library, you can think of it like extracting the tarball back to object .o files, then only linking the object files you need into the final binary.

Here's an example.

sourceA.c contains FunA1() and FunA2().
sourceB.c contains FunB()

The library file source.a contains sourceA.o and sourceB.o.

The user is ONLY USING FunA1() from the library. Since the sourceA.o is needed, the entire object files contents ends up in the final binary, even though FunA2() isn't actually needed. Nothing from SourceB.o was needed, so FunB() will not end up in the binary.

Thus, when designing a static library, think about how you break up the functions into source files if you are concerned about the size of the final binary.
 
Yes, that's pretty much it. Often the term "compile unit" is used for the all-or-nothing chunk of data which the linker must include.

By default, Arduino uses "-ffunction-sections -fdata-sections" as it compiles code. This is supposed to cause each function and each global scope variable to become its own compile unit. It greatly helps reduce code size without the need to create so many very small files. Much of the advise you'll see on general non-Arduino programming sites is written for the compiler's default where everything from a single .c or .cpp file becomes a .o file with only 1 compile unit.
 
Thanks Paul, that's good to know! That will save me from having to break up my library into more source files than I would otherwise like.
 
Yes, that's pretty much it. Often the term "compile unit" is used for the all-or-nothing chunk of data which the linker must include.

By default, Arduino uses "-ffunction-sections -fdata-sections" as it compiles code. This is supposed to cause each function and each global scope variable to become its own compile unit. It greatly helps reduce code size without the need to create so many very small files. Much of the advise you'll see on general non-Arduino programming sites is written for the compiler's default where everything from a single .c or .cpp file becomes a .o file with only 1 compile unit.

To be pedantic, in addition to using "-ffunction-sections -fdata-sections" when it compiles code, Arduino needs to pass -Wl,--gc-sections when linking, so that the linker will actually delete the various sections that aren't used by active code.

Thanks Paul, that's good to know! That will save me from having to break up my library into more source files than I would otherwise like.
If you can, be sure to compile your library with "-ffunction-sections -fdata-sections", to allow unused functions and data to be discarded.
 
Status
Not open for further replies.
Back
Top