Microchip Technology SW006022-2N Data Sheet

Page of 338
MPLAB
®
 XC16 C Compiler User’s Guide
DS52071B-page 54
 2012 Microchip Technology Inc.
3.3.2
Single-Step Compilation
A single command-line instruction can be used to compile one file or multiple files.
3.3.2.1
COMPILING A SINGLE FILE
This section demonstrates how to compile and link a single file. For the purpose of this 
discussion, it is assumed the compiler is installed in the standard directory location and 
that your PATH or other environment variables (see Section 3.2.2 “Environment Vari-
ables”
) are set up in such a way that the full compiler path need not be specified when 
you run the compiler.
The following is a simple C program that adds two numbers.
Create the following program with any text editor and save it as ex1.c.
#include <xc.h>
int main(void);
unsigned int Add(unsigned int a, unsigned int b);
unsigned int x, y, z;
int 
main(void)
{
  x = 2;
  y = 5;
  z = Add(x,y);
  return 0;
}
unsigned int 
Add(unsigned int a, unsigned int b)
{
  return(a+b);
}
The first line of the program includes the header file xc.h, which will include the appro-
priate header files that provides definitions for all special function registers on the target 
device. For more information on header files, see Section 4.3 “Device Header Files”.
Compile the program by typing the following at the prompt in your favorite terminal.
xc16-gcc -mcpu=30f2010 -o ex1.out ex1.c
The command-line option -o ex1.out names the output executable file (if the -o 
option is not specified, then the output file is named a.out). The executable file may 
be loaded into the MPLAB IDE.
TABLE 3-3:
COMPILER APPLICATION NAMES
Name
Description
xc16-gcc
Command line driver; the interface to the compiler
xc16-cc1
Code generator (elf by default)
xc16-as
Assembler (based on the target device)
xc16-ld
Linker
xc16-bin2hex
Conversion utility to create HEX files
xc16-strings
String extractor utility
xc16-strip
Symbol stripper utility
xc16-nm
Symbol list utility
xc16-ar
Archiver/Librarian
xc16-objdump
Object file display utility
xc16-ranlib
Archive indexer utility