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 variable. The following are other directories of note:

  • <install-dir>/pic32c/include - the directory for standard C header files.
  • <install-dir>/pic32c/include/proc - the directory for PIC32 device-specific header files.
  • <install-dir>/pic32c/lib - the directory structure for standard libraries and start-up files.
  • <install-dir>/pic32c/lib/proc - the directory for device-specific linker script fragments, register definition files and configuration data may be found.

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*/
// ATSAME70N20B Configuration Bit Settings

// Config Source code for XC32 compiler.
// GPNVMBITS
#pragma config SECURITY_BIT = CLEAR
#pragma config BOOT_MODE = CLEAR
#pragma config TCM_CONFIGURATION = 0x0 // Enter Hexadecimal value

// ONLY Set LOCKBITS are generated.
// LOCKBIT_WORD0

// LOCKBIT_WORD1

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#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.

Compile the program by typing the following at the prompt:

xc32-gcc -mprocessor=ATSAME70N20B -o ex1.elf ex1.c

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.

If a hex file is required, for example, to load into a device programmer, then use the following command:

xc32-bin2hex ex1.elf

This 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*/
// ATSAME70N20B Configuration Bit Settings

// Config Source code for XC32 compiler.
// GPNVMBITS
#pragma config SECURITY_BIT = CLEAR
#pragma config BOOT_MODE = CLEAR
#pragma config TCM_CONFIGURATION = 0x0 // Enter Hexadecimal value
// ONLY Set LOCKBITS are generated.
// LOCKBIT_WORD0
// LOCKBIT_WORD1
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#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);
}

Compile both files by typing the following at the prompt:

xc32-gcc -mprocessor=ATSAME70N20B -o ex1.elf ex1.c add.c

This 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.