Creating and using libraries
Using libraries
Creating a new project
Creating a library project
Using the library in your application project
* This tutorial demonstrates how to create a library project and how you can combine it with an application project.
* This tutorial assumes that you are familiar with the basics of the IAR Embedded Workbench® IDE described in the previous tutorials.
Using libraries
If you are working on a large project, you will soon accumulate a collection of useful modules that contain one or more routines to be used by several of your applications. To avoid having to assemble or compile a module each time it is needed, you can store such modules as object files, that is, assembled or compiled but not linked.
You can collect many modules in a single object file which then is referred to as a library. It is recommended that you use library files to create collections of related routines, such as a device driver.
Use the IAR Archive Tool iarchive to build libraries.
The Main.s program
The Main.s program uses a routine called max to set the contents of the register R1 to the maximum value of the word registers R1 and R2. The EXTERN directive declares max as an external symbol, to be resolved at link time.
A copy of the program is provided in the arm\tutor directory.
The library routines
The two library routines will form a separately assembled library. It consists of the max routine called by main, and a corresponding min routine, both of which operate on the contents of the registers R1 and R2 and return the result in R1. The files containing these library routines are called Max.s and Min.s and copies are provided in the arm\tutor directory.
The PUBLIC directive makes the max and min symbols public to other modules.
For detailed information about the PUBLIC directive, see the ARM® IAR Assembler Reference Guide.
Creating a new project
1
In the workspace tutorials used in previous tutorials, add a new project called project5.
2
Add the file Main.s to the new project.
3
To set options, choose Project>Options. Select the General Options category and click the Library Configuration tab. Choose None from the Library drop-down list, which means that a standard C/C++ library will not be linked.
On the General Options>Target page, choose Cortex-M3 from the Core drop-down list.
The default options are used for the other option categories.
4
To assemble the file Main.s, choose Project>Compile.
You can also click the Compile button on the toolbar.
Creating a library project
Now you are ready to create a library project.
1
In the same workspace tutorials, add a new project called tutor_library.
2
Add the files Max.s and Min.s to the project.
3
To set options, choose Project>Options. In the General Options category, verify these settings:
 
Note that Library Builder appears in the list of categories, which means that the IAR Archive Tool iarchive is added to the build toolchain. You do not have to set any iarchive-specific options specific for this tutorial.
Click OK.
4
Choose Project>Make.
The library output file tutor_library.a has now been created in the projects\Debug\Exe directory.
Using the library in your application project
Now add your library containing the maxmin routine to project5.
1
In the Workspace window, click the project5 tab. Choose Project>Add Files and add the file tutor_library.a (tutor_library.a for ILINK) located in the projects\Debug\Exe directory. Click Open.
2
Click Make to build your project.
3
You have now combined a library with an executable project, and the application is ready to be executed. For information about how to manipulate the library, see the IAR Archive Tool documentation available in the IAR C/C++ Development Guide for ARM®.