Microchip Technology SW006023-2N Data Sheet

Page of 238
MPLAB
®
 XC32 C/C++ Compiler User’s Guide
DS51686E-page 50
 2012 Microchip Technology Inc.
The first line of the program includes the header file xc.h, which provides definitions 
for all Special Function Registers (SFRs) on that part.
 Compile the program by typing the following at the prompt:
xc32-gcc 
–mprocessor=32MX795F512L 
-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 MPLAB IDE.
If a hex file is required, for example, to load into a device programmer, then use the 
following command:
xc32-bin2hex ex1.out
This creates an Intel hex file named ex1.hex.
3.3.1.2
COMPILING MULTIPLE C FILES
This section demonstrates how to compile and link multiple files in a single step. Move 
the Add() function into a file called add.c to demonstrate the use of multiple files in 
an application. That is:
File 1
/* ex1.c */
#include <xc.h>
#include <plib.h>
// Device-Specific Configuration-Bit settings
// SYSCLK = 80 MHz (8MHz Crystal/ FPLLIDIV * FPLLMUL / FPLLODIV)
// PBCLK = 40 MHz
// Primary Osc w/PLL (XT+,HS+,EC+PLL)
// WDT OFF
// Other options are don't care
//
#pragma config FPLLMUL = MUL_20, FPLLIDIV = DIV_2, FPLLODIV = DIV_1, 
FWDTEN = OFF
#pragma config POSCMOD = HS, FNOSC = PRIPLL, FPBDIV = DIV_8
int main(void);
unsigned int add(unsigned int a, unsigned int b);
unsigned int x, y, z;
int main(void)
{
  /* Configure the target for maximum performance at 80 MHz. */
  SYSTEMConfigPerformance(80000000UL);
  x = 2;
  y = 5;
  z = Add(x,y);
  return 0;
}
File 2
/* add.c */
#include <xc.h>
unsigned int 
add(unsigned int a, unsigned int b)
{
  return(a+b); 
}