Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 11 of 11

Thread: How to add a pre-compiled library?

  1. #1
    Junior Member
    Join Date
    May 2017
    Posts
    9

    How to add a pre-compiled library?

    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

  2. #2
    Senior Member+ manitou's Avatar
    Join Date
    Jan 2013
    Posts
    2,527
    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

  3. #3
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    22,080
    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?

  4. #4
    Senior Member+ manitou's Avatar
    Join Date
    Jan 2013
    Posts
    2,527
    Quote Originally Posted by PaulStoffregen View Post
    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/w...es-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 by manitou; 03-06-2018 at 07:23 PM.

  5. #5
    Junior Member
    Join Date
    May 2017
    Posts
    9
    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.t xt

    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.

  6. #6
    Senior Member+ manitou's Avatar
    Join Date
    Jan 2013
    Posts
    2,527
    Quote Originally Posted by sevenEleven View Post
    Of course this solution is suboptimal since from now on mylib is linked into every teensy31/32 project.
    No harm. If your sketch doesn't have any references to symbols in your mylib, nothing will be linked in

  7. #7
    Senior Member Blackaddr's Avatar
    Join Date
    Mar 2017
    Location
    Canada
    Posts
    262
    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.

  8. #8
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    22,080
    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.

  9. #9
    Senior Member Blackaddr's Avatar
    Join Date
    Mar 2017
    Location
    Canada
    Posts
    262
    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.

  10. #10
    Senior Member+ MichaelMeissner's Avatar
    Join Date
    Nov 2012
    Location
    Ayer Massachussetts
    Posts
    3,743

    Cool

    Quote Originally Posted by PaulStoffregen View Post
    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.

    Quote Originally Posted by Blackaddr View Post
    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.

  11. #11
    Senior Member
    Join Date
    Apr 2013
    Posts
    151
    An support for precompiled would be nice

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •