Creating and using Libraries with ARM gcc and Eclipse

In ‘A Library with ARM gcc and Eclipse’ I was using the CodeWarrior MCU10.3 beta version to create a library project. At that time I had to do things manually. Now with the final MCU10.3 there is an option in the New Project Wizard which makes things easier:

Library Creation

Library Creation

This will create a library (or better: an archive) with gcc for me. But how to use it from another project?

In my library project I have a simple test function

int myFunction(void);

which I want to use from my application. So I implement this function in my library project:

Library Project

Library Project

Note that it creates a library named libLibrary.a. This file is physically inside the Debug output folder:

libLibrary archive in debug folder

libLibrary archive in debug folder

To use the library function in another project, I need

  1. to include the library header file
  2. use the function

So this could look like this:

#include "myLibrary.h"

int i;

int main(void) {
i = myFunction();
for(;;);
return 0;
}

In my example, I have both the application project (which is uses the library) and the library project in the same workspace.

Application and Library Project

Application and Library Project

First, I need to tell the compiler where the header file can be found. This needs to be added to the Include paths of the compiler:

Added Include Path

Added Include Path

Now I need to tell the GCC linker to use my library. I need to specify the library name (-l option) and the library search path (-L option). The library search path is the path were the library physically is. And I need to list the library name in the Libaries list (top box below):

GCC Linker Options for the Library

GCC Linker Options for the Library

❗ Very important: the -l option lists the names *without* the ‘lib’ prefix and the ‘.a’ extension. So if the library is libTest.a, then only ‘Test’ has to be specified. I cannot tell how many times I was trapped here :-(.

Ok, now with this it should be possible to link with the library :-).

Happy Archiving 🙂

50 thoughts on “Creating and using Libraries with ARM gcc and Eclipse

  1. Pingback: Tutorial: Using the ARM CMSIS Library | MCU on Eclipse

  2. I can’t thank you enough for the “very important” note at the end of the article. That caught me up for 2 hours.
    FYI: Everything in this article still holds true for CW 10.5

    Like

  3. Hi Erich,
    After this can we use the function directly in the main file right.

    But i am not able to access the functions there, Please help.

    Like

  4. Hi Erich,

    When I include the library header file to use the library function in my project I get the following message: Unresolved inclusion: “myLibrary.h”. (a question mark icon appears nexto the text)

    I followed all the steps of your post adding library paths and so on. And even if I run the code step by step I can see that my library function is called and executed.

    Even though, if I try to open de function or header file declaration from the project it is not possible. I get the following error message at the bottom CW status bar (in red):

    Could not find symbol ‘myFunction’ in index or ould not find symbol ‘myLibrary.h’ in index

    I’m working with CW 10.5.

    Could you help me please? Thanks in advance

    Like

  5. Pingback: Creating a Library with Kinetis Design Studio | MCU on Eclipse

  6. Hi Erich,

    I use Eclipse, gcc(MinGW), gdb for the library project.
    I wanted to check the coverage of my library project(contains only one C file(simplecalc.c)without main and few header files).
    I have another executable project in the same workspace having test.c containing the test cases for the library project. To use gcov I had to copy content of simplecal.c into test.c and was successful to see the coverage(with green and red colors and also the percentage of coverage).
    But, I want to know, Is there any other option to achieve the same without modifying test.c.
    PS: I did this not on any target but offline. I have already saw the video on “code coverage with Eclipse and gcov” which I would try later.

    Like

  7. Thank God for you, i have struggled for a few minutes with TSS library component. The part about what to include on the library name was my problem. I read your comment somewhere that you stopped using TSS, what’s your no. 1 alternative.

    Like

    • I’m not using any more capacitive touch sensing/electrodes. But what worked well was using external components, like the MPR083 and MPR084 device. I have created a Processor Expert component for it too (MPR08x).

      Like

  8. I’ve followed your instructions to the letter, but I’m still getting “undefined references” to functions that I have confirmed are in my library file.

    Any other suggestions?

    Like

    • Hi Michael,
      if it has undefined references, then the needed symbols are somehow not present/experted/listed in the library interface.
      Are you sure your functions are not marked ‘static’? Or are you using C++ (then the name mangling is added there).
      Can you use the ‘nm’ utility (see https://mcuoneclipse.com/2012/11/01/defining-variables-at-absolute-addresses-with-gcc/) on your library to see if the necessary symbols are present?

      Like

      • Hi Erich, I ran “nm” and yes the symbol is there. It’s a function called “WM_GetDialogItem”.
        In several places of the nm output I see:

        U WM_GetDialogItem

        And the .o file the function resides in is listed in the output as well.

        Yes, my application is C++, and the library is all C functions. But, I was compiling the library as part of my application originally, and had no problems. I just decided to make it a library to cut down on the number of .c files in my project.

        I believe we are having the same discussion over on freescale forum 🙂

        I’m really out of ideas

        Like

  9. Hi everybody
    I started three years ago developing a project using CW10.3 and ARM Toolchain, so now how can I add a already compiled library file (xx.a) to my project?
    Many thanks for your help.
    Regards
    Mauro Righetto

    Like

    • In general I recommend to build the library with the same compiler(s) to avoid any incompatibilities. Your library needs to be named libxx.a (not xx.a). See at the end of this article how the naming convention is used.
      I hope this helps,
      Erich

      Like

  10. Pingback: Building the NXP BLE Stack with Open Source GNU and Eclipse Tools | MCU on Eclipse

  11. Pingback: “No source available”, or how to Debug Multiple Binaries with GDB and Eclipse | MCU on Eclipse

  12. Hi Erich Styger,

    I am using 10.6 and i am following same steps which you mentioned but i am not getting “Directories” under “gcc c compiler”….Please help me.

    Thanks in Advance.

    Like

    • Are using a GNU/gcc project for ARM/Kinetis? I tried CW 10.6.4, and that entry is there for me, and has not changed for a long time to my knowledge. But if you are using a different toolchain/compiler, things might be different.

      Like

      • Hi Erich,

        While creating new project i selected ARM Build tools as “Frescale” instead of “GCC” under “Language and Build tools options”.
        Here i am getting “Inputs” instead of “Directories”.

        Please suggest me how to create library for Freescale and i am using MK22FX512 ic.

        Thanks in advance

        Like

        • Hi Sidaray,
          to my knowledge, the ‘Freescale’ tool chain is legacy and in maintenance mode. I always have used the GCC one. But the settings are similar.

          Like

  13. I have a problem including the GNU Scientific Library in my project. I followed the steps indicated, but still I get a “undefined reference to `func_name’ “. I downloaded the pre-compiled library from here: https://sourceforge.net/projects/gnu-scientific-library-windows/
    and used the “static” folder.
    Under “Cross ARM C compiler” I included the paths
    “${workspace_loc:/${ProjName}/gsl-2.2.1-static/include}”
    “${workspace_loc:/${ProjName}/gsl-2.2.1-static/lib}”
    under “Cross ARM C++ Linker” I included “gls” and “glscblas” in libraries and
    “${workspace_loc:/${ProjName}/gsl-2.2.1-static/include}”
    “${workspace_loc:/${ProjName}/gsl-2.2.1-static/lib}”
    in library search paths. All my files include the following includes
    #include
    #include
    #include

    but still when I compile I get the error. The program can find the headers (i.e. if I “go to declaration” on a library function the right file is opened.

    Like

    • I don’t see anything wrong with your paths (assuming that you really have it installed inside these folders), an if you have the paths present in the -I compiler option. I would check the actual compiler command line in the console view. And what is excactly your compiler error? If the compiler issues an error, then the link phase does not matter (yet). The other thought is: if you are using Windows, you might face a problem with the lenght of the command line? see https://mcuoneclipse.com/2015/03/29/solving-the-8192-character-command-line-limit-on-windows/

      Like

    • One thing to add: if Eclipse (“go to declaration”) finds the file, that means the file exists on disk in a folder Eclipse can see with the project folders. But does not tell that the compiler can find it, as the compiler has only the information passed to it through the command line options.

      Like

  14. Pingback: Tutorial: Secure TLS Communication with MQTT using mbedTLS on top of lwip | MCU on Eclipse

  15. Pingback: How to use Custom Library Names with GNU Linker and Eclipse | MCU on Eclipse

  16. Hi Erich
    I’ve followed all your steps in sequence but it still cannot work. Furthermore, I also used the very important note and I couldn’t figure out the reason. My object is based on MCUXpresso 11.
    PS: my ERROR is like this.
    (execution path)…cannot find -l…(library path)
    Even if I open a new workspace to build it, it doesn’t work and shows the same errors.

    Like

    • I would be surprised if this would be someting with the workspace. Is the library you want to link with listed in the -L (library search path) linker option? Could you post your exact error message?

      Like

      • Yes, my library did has path in the -L linker option. However, even if I deleted the -L library path, it didn’t work and the errors were the same. The errors follow.

        d:/nxp/mcuxpresso_ide/mcuxpressoide_11.0.0_2516/ide/plugins/com.nxp.mcuxpresso.tools.win32_11.0.0.201905131304/tools/bin/../lib/gcc/arm-none-eabi/8.2.1/../../../../arm-none-eabi/bin/ld.exe: cannot find -lD:/NXP/LPC84x_SAKEE-master/peripherals_lib/peripherals_lib/flash/peripherals_lib
        d:/nxp/mcuxpresso_ide/mcuxpressoide_11.0.0_2516/ide/plugins/com.nxp.mcuxpresso.tools.win32_11.0.0.201905131304/tools/bin/../lib/gcc/arm-none-eabi/8.2.1/../../../../arm-none-eabi/bin/ld.exe: cannot find -lD:/NXP/LPC84x_SAKEE-master/utilities_lib/utilities_lib/flash/utilities_lib
        collect2.exe: error: ld returned 1 exit status
        make: *** [makefile:33: Example_SPI_OLED_SSD1306.axf] Error 1

        There are three errors totally. I cannot figure out the reasons. Thanks for your attention.

        Mike

        Like

        • Please have a look at

          The -L option is used for the *directory* where the library is located
          The -l option is used to specify the library name *without* the lib and the .a
          I hope this helps.

          Like

  17. Erich,
    I think I use -L option and -l option in true way. My -L option has pointed out the library path and two libraries do exist in the paths. I followed your special steps carefully and still didn’t work.

    -l:
    “D:\NXP\LPC84x_SAKEE-master\peripherals_lib\peripherals_lib\flash\peripherals_lib”
    “D:\NXP\LPC84x_SAKEE-master\utilities_lib\utilities_lib\flash\utilities_lib”
    -L:
    “D:\NXP\LPC84x_SAKEE-master\peripherals_lib\peripherals_lib\flash”
    “D:\NXP\LPC84x_SAKEE-master\utilities_lib\utilities_lib\flash”

    To be honest, I really have no idea about this problem as I have concentrated on it for two whole days. It is very important to me. Thanks for your attention!

    Mike

    Like

  18. Thanks~

    “Very important: the -l option lists the names *without* the ‘lib’ prefix and the ‘.a’ extension. So if the library is libTest.a, then only ‘Test’ has to be specified. I cannot tell how many times I was trapped here :-(.”

    Like

What do you think?

This site uses Akismet to reduce spam. Learn how your comment data is processed.