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:
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:
Note that it creates a library named libLibrary.a. This file is physically inside the Debug output folder:
To use the library function in another project, I need
- to include the library header file
- 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.
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:
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):
❗ 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 🙂
Pingback: Tutorial: Using the ARM CMSIS Library | MCU on Eclipse
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
LikeLike
You are welcome :-).
And yes, things are still the same for CW 10.5 as nothing has been changed in the ARM GNU tools for this.
LikeLike
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.
LikeLike
I got it working!!!!!
Thanks a lot for the blog!!!!!
LikeLike
great! and what was the problem?
LikeLike
Hi Erich,
Everything was working fine and all of a sudden it is actually throwing an Error like
Description Resource Path Location Type
cannot find -lusing_Library_Files I2C_Drivers_KL25Z C/C++ Problem
Help, Cannot find what is the error
LikeLike
Maybe your command line length is too long? In Windows there is a limit of 8192 characters max.
LikeLike
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
LikeLike
Hi Marc,
can you try to rebuild the index? See https://mcuoneclipse.com/2012/03/20/fixing-the-eclipse-index/
Erich
LikeLike
Awesome! it’s working now.
I needed to “Allow heuristic resolution of includes” and “Use active build configuration”. Solved, thanks!
LikeLike
Pingback: Creating a Library with Kinetis Design Studio | MCU on Eclipse
good one
LikeLike
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.
LikeLike
Hi RaTh,
I’m not sure why you had to modify test.c?
Erich
LikeLike
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.
LikeLike
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).
LikeLike
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?
LikeLike
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?
LikeLike
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
LikeLike
can you post in the forum the full linker command line and error messages?
Maybe I see something.
Erich
LikeLike
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
LikeLike
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
LikeLike
Pingback: Building the NXP BLE Stack with Open Source GNU and Eclipse Tools | MCU on Eclipse
Pingback: “No source available”, or how to Debug Multiple Binaries with GDB and Eclipse | MCU on Eclipse
Hi Everyone,
I am using CW 10.6. Can you tell me how to include library files to other projects?
LikeLike
Hi Mayur,
see the information at the end of this article, especially the picture “GCC Linker Options for the Library”. This is all the same for all CodeWarrior for MCU10.x.
LikeLike
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.
LikeLike
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.
LikeLike
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
LikeLike
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.
LikeLike
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.
LikeLike
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/
LikeLike
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.
LikeLike
Pingback: Tutorial: Secure TLS Communication with MQTT using mbedTLS on top of lwip | MCU on Eclipse
Pingback: How to use Custom Library Names with GNU Linker and Eclipse | MCU on Eclipse
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.
LikeLike
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?
LikeLike
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
LikeLike
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.
LikeLike
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
LikeLike
I tried your special attention when I first saw it. It cannot work.
LikeLike
what are the names of your libraries on disk?
LikeLike
libperipherals_lib.a
libutilities_lib.a
LikeLike
So use
-lperipherals_lib
-lutilities_lib
-L"D:\NXP\LPC84x_SAKEE-master\peripherals_lib\peripherals_lib\flash"
-L"D:\NXP\LPC84x_SAKEE-master\utilities_lib\utilities_lib\flash"
LikeLike
With the -l (lower case L!) your are supposed to pass a file name, not a path. Your example above shows that you are using a path which is wrong.
LikeLike
I see. It could build now. For the -l option the blank wants me to browse a file and I follow it. Then it showed the path. Now I change them into the file names and it works! Thank you very much!
Mike
LikeLiked by 1 person
great!
LikeLike
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 :-(.”
LikeLike
Glad to hear it was useful to know this 🙂
LikeLike