PICmicro & dsPIC >> PIC F.A.Q. (Frequently Asked Questions)

Pages: 1
Mark PappinAdministrator


Reged: Nov 01 2004
Posts: 599
Loc: Brisbane, Australia
Creating and using library files
      #29192 - Tue Aug 14 2007 04:10 PM

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

Post Extras: Print Post   Remind Me!   Notify Moderator  

Reged: Jan 23 2007
Posts: 55
Loc: Stillwater, OK
Re: Creating and using library files [Re: Mark Pappin]
      #76992 - Tue Oct 07 2008 12:24 AM

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?



Edited by _Ed (Tue Oct 07 2008 12:48 AM)

Post Extras: Print Post   Remind Me!   Notify Moderator  
Pages: 1

Extra information
0 registered and 0 anonymous users are browsing this forum.

Moderator:  Andrew L 

Print Topic

Forum Permissions
      You cannot start new topics
      You cannot reply to topics
      HTML is enabled
      UBBCode is enabled

Rating: ****
Topic views: 28667

Rate this topic

Jump to

Contact Us | Privacy statement HI-TECH Software

Powered by UBB.threads™ 6.5.5