Relocatable Objects / Multiple Compilation Unit (IDE Only)

There are several methods of allowing you to create relocatable objects and link them together:

  1. Inside PCW, the CCS IDE, you can use the file navigator to add units to your project. When you press Build it will compile all your units and link them together.


  2. #import() and #export() preprocessor commands allow you to create units and link units at the source code level.
    /*
    The following preprocessor command will make the compiler compile this code into a relocatable object file (.o).

    RELOCATABLE will create a CCS relocatable object file, and is the default format when #EXPORT is used.

    FILE specifies the name of the output file, and is optional.

    The ONLY option tells the compiler to only make the symbol GetString (which could be a function or variable) visible to modules that import/link this module.

    Another option, EXCEPT, has the opposite effect - all symbols except the listed symbols are visible to modules that import/link this module.
    */

    #export(RELOCATABLE, FILE=getstring.o, ONLY=GetString)

    /*
    The following preprocessor command will make the compiler link other objects into this unit (in this example it will link unit.o).

    FILE, ONLY, and EXCEPT have the same operation as detailed in the previous example.

    COFF is the reciprocal of RELOCATABLE, and tells the compiler to link/import an .o created by MPASM. C18 and C30. COFF can only be used with #import, it cannot be used with #export.
    */

    #import(RELOCATABLE, FILE=unit.o)

    As shown in the previous examples #import and #export have options for defining scope, but the standard C qualifiers static and extern can also be used for defining scope.


  3. Command-line options have been added to create units (instead of a compile/link in one step) and link all units into one final HEX. The following example shows compiling two .C files into seperate .O files and then linking them into one .HEX file:
    C:\project\ccsc +FH +EXPORT main.c
    C:\project\ccsc +FH +EXPORT uart.c
    C:\project\ccsc +FH LINK="main.hex=main.o,uart.o"

  4. Inside MPLAB® IDE, if you add more than one .C in the project manager all the .C files will be compiled seperately into relocatable objects, and in the final step all relocatable objects are linked into one HEX.
* Command-Line compiler customers can only link, they cannot create relocatable objects.
   Relocatable Objects/Linker functions work only with the CCS C Windows IDE.

C-Aware IDE Demo
Embedded C Learners Kit
C Workshop Compiler