?
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
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.
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.
I got it working!!!!!
Thanks a lot for the blog!!!!!
great! and what was the problem?
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
Maybe your command line length is too long? In Windows there is a limit of 8192 characters max.
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
Hi Marc,
can you try to rebuild the index? See
Erich
Awesome! it’s working now.
I needed to “Allow heuristic resolution of includes” and “Use active build configuration”. Solved, thanks!
Pingback: Creating a Library with Kinetis Design Studio | MCU on Eclipse
good one
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.
Hi RaTh,
I’m not sure why you had to modify test.c?
Erich
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.
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).
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?
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) on your library to see if the necessary symbols are present?
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
can you post in the forum () the full linker command line and error messages?
Maybe I see something.
Erich
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
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
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?
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.
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.
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.
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
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.
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:
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.
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
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.
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