Building XC8 Libraries

Last modified by Microchip on 2024/06/24 06:33

Recent versions of MPLAB® X IDE now allow you to create library projects for the MPLAB XC8 compiler. The process of creating and building a library project for this compiler is the same as that for the other MPLAB XC compilers, and that process is discussed on the "Libraries in MPLAB X IDE" page. There are, however, a few subtle differences between the libraries produced by MPLAB XC8 and those of the other Microchip compilers. These differences are discussed in this article.

MPLAB XC8 library files use their own format, which is different from that produced by the other MPLAB XC compilers, and which is also different from that used by the legacy MPLAB C18 compiler. MPLAB XC8 libraries use the extension LPP. Do not rename or specify these files to use any other extension, such as LIB. The IDE will produce a library with the correct extension when you build a library project. If your project is called MyLib, the output library file will be called MyLib.X.lpp.

The LPP libraries built by MPLAB XC8 must be built from only C source files. Libraries built from assembly code must be encoded in a different format, must use the traditional LIB extension, and must be built by the compiler on the command line. You cannot build any library from a mix of C and assembly sources.

When you are using a library built with MPLAB XC8, you can perform C-level debugging (e.g., C-line stepping, watching variables, etc.,) on the routines and variables defined in the library routines, provided the source code for the library is available and in the correct location. You will need to place the library source files so that the relative path between their location and the project that is using them is the same as the relative path between where the library build command was executed and where the source files were located when they were built. Assuming that you built the library from source files that were placed into the library’s X project directory, then you simply need to copy the source files into the X directory of the project that is using the library for debugging to work correctly. If the source files are not available, the library code will still work as expected, but you will lose the ability to debug the library routines.

Although you can now easily build libraries from within MPLAB X IDE, you can continue to build libraries on the command line using the XC8 compiler, if you prefer. This can be performed in just one step. For example, to build the source code contained in adc.clcd.c, and init.c into a library, the following command line will perform this task.

xc8 chip=18f4520 output=lpp init.c adc.c lcd.

This will create an output called init.lpp, which you can rename if required. Alternatively, you can specify an output file name using the compiler’s "-O" option, for example, -OMyLib.lpp.