5.3.1 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/c++ - 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.

The following is a simple C++ program. Create the following program with any plain-text editor and save it as ex1.cpp.

File 1

/* ex1.cpp */

// ATSAME70Q21B Configuration Bit Settings
#pragma config SECURITY_BIT = CLEAR
#pragma config BOOT_MODE = CLEAR
#pragma config TCM_CONFIGURATION = 0x0 // Enter Hexadecimal value

#include <xc.h>
#include <iostream> 
using namespace std;

unsigned int add(unsigned int a, unsigned int b)
{ 
    return (a + b);
}
int main(void)
{
    int myvalue = 6;

    std::cout << "original value: " << myvalue << endl;
    myvalue = add(myvalue, 3);
    std::cout << "new value: " << myvalue << endl;
    while (1);
}

The program includes the header file xc.h, which provides definitions for all Special Function Registers (SFRs), or as they are sometimes called, peripheral registers, on the target device. The <iostream> header file provides the necessary prototypes for the peripheral library. For completion, the user must provide actual implementations of functions _mon_getc and _mon_putc for file IO. By default the XC32 compiler links do-nothing stubs for these functions.

Compile the program by typing the following at a command prompt.

xc32-g++ -mprocessor=ATSAME70Q21B -Wl,--defsym=_min_heap_size=0xF000 -o ex1.elf ex1.cpp

The option -o ex1.elf names the output executable file. This elf file may be imported into MPLAB X IDE.

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.