Mark Pappin
 
Reged: Nov 01 2004
Posts: 599
Loc: Brisbane, Australia
|
|
In normal operation, our standard compilers compile C and assembler source files into object files, and then link those object files together, along with the system libraries, to produce Hex and COFF files which you use to program your chip.
Sometimes you might want to create your own library files. We have long provided the LIBR tool to manipulate library files, but now (that is, since version 9.50 of our PIC-family compilers) there's an easier way: use --output=LIB on the command line of the Link phase to create a library file instead of a Hex or COFF file.
To illustrate how this would be used in practice, consider an MPLAB project called App with a number of source files where some of these source files are used across multiple projects - say, the code to control an LCD display.
- First, create a new project LCD with the same build options, but referencing only the source and header files in your LCD (you could save App under a new name, then delete the non-LCD files from this new project).
- Add --output=LIB to the Linker options tab (or the Global tab, in the new Universal toolsuite) for project LCD.
- Build this project to create the .LIB file. MPLAB will likely complain that the build failed, as no .COF file was produced (if it does not complain, it may have found a .COF file from a previous build) - this is not a problem at this stage.
- Then, reload App, and remove from it any source files that you have compiled in LCD.
- Add the .LIB file created by LCD to the Library Files section of App
- Build App
It's important to remember that library files are only searched for functions and variables actually referenced from source files within your project, so you should ensure that your main() function and any interrupt service routines are not put into a library.
Note also that both the library and any application you link it with must be built with the same compiler options. Some option mismatches can be caught by the linker (e.g. differences in double width, and possibly code-pointer size) but not all of them can, and the diagnostic messages for those that are detected won't necessarily say "build option mismatch between library and application".
-------------------- Mark Pappin - HI-TECH Software
|
_Ed
Tester
Reged: Jan 22 2007
Posts: 55
Loc: Stillwater, OK
|
|
Thank you for the excellent tips.
Does a library have to include a "main" function? Or is the missing main due to the MPLAB IDE? I made an empty main.c just to make it happy.
Do I need to make a library for each device type? Or can it be a generic PIC18F as long as it is not doing I/O?
I am writing test cases using the simulator for my library routines. Is it possible to source level debug into a library routine?
Thanks,
-Ed
Edited by _Ed (Mon Oct 06 2008 10:48 AM)
|
|