5.2.2 Single-Step C Compilation
A single command-line instruction can be used to compile one file or multiple files.
COMPILING A SINGLE C FILE
This section demonstrates how to compile and link a single file. For the purpose
of this discussion, it is assumed the compiler's
<install-dir>/bin directory has been added to your
PATH environment variable. The following are other directories of
note:
<install-dir>/pic32c/include- the directory for standard C header files.<install-dir>/pic32c/lib- the directory structure for standard libraries and start-up files.
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.
/* ex1.c*/
// Config Source code for XC32 compiler.
#pragma config WDT_ENABLE = OFF // Watchdog Timer: Disabled
#pragma config BOD33_DIS = DISABLED // BOD33: Disabled
#pragma config NVMCTRL_REGION_LOCKS = 0xFFFF // NVM region locks: None
#include <xc.h>
unsigned int x, y, z;
unsigned int add(unsigned int a, unsigned int b) {
return (a + b);
}
int main(void) {
x = 2;
y = 5;
z = add(x, y);
return 0;
}The program includes the header file xc.h, which provides definitions for all Special Function Registers (SFRs) on that part. Some documentation uses the term "peripheral registers" to describe these device registers.
xc32-gcc -mprocessor=ATSAME54P20A
-mdfp="Microchip/SAME54_DFP/3.9.244" -o ex1.elf ex1.cDownload the required DFP for your selected target device and specify this with
the -mdfp option (see Dfp Option). The command line
option -o ex1.elf 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 imported into MPLAB X IDE with
File-Import-Hex/Elf (prebuilt) File.
xc32-bin2hex ex1.elfThis creates an Intel® Hex file named
ex1.hex.
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*/
#pragma config WDT_ENABLE = OFF // Watchdog Timer: Disabled
#pragma config BOD33_DIS = DISABLED // BOD33: Disabled
#pragma config NVMCTRL_REGION_LOCKS = 0xFFFF // NVM region locks: None
#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;
}File 2
/* add.c */
#include <xc.h>
unsigned int add(unsigned int a, unsigned int b) {
return(a+b);
}xc32-gcc -mprocessor=ATSAME54P20A
-mdfp="Microchip/SAME54_DFP/3.9.244" -o ex1.elf ex1.c add.cThis command compiles the modules ex1.c and add.c. The compiled modules are linked with the compiler libraries and the executable file ex1.elf is created.
