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/lib- the directory structure for standard libraries and start-up files.
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 */
#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>
#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.
xc32-g++ -mprocessor=ATSAME54P20A
-mdfp="Microchip/SAME54_DFP/3.9.244" -Wl,--defsym=_min_heap_size=0xF000 -o ex1.elf ex1.cppThe option -o ex1.elf names the output executable file. This elf file may be imported into MPLAB X IDE.
xc32-bin2hex ex1.elfThis creates an Intel hex file named ex1.hex.
