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 🙂

24 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

What do you think?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s