5.3.2 Compiling Multiple C++ Files

This section demonstrates how to compile and link multiple C and C++ files in a single step.

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;

extern unsigned int add(unsigned int a, unsigned int 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);
}

File 2

/* add.cpp */
unsigned int add(unsigned int a, unsigned int b) {
  return(a+b);
}
Compile both files by typing the following at the prompt:
xc32-g++ -mprocessor=ATSAME54P20A
                                        -mdfp="Microchip/SAME54_DFP/3.9.244" -Wl,--defsym=_min_heap_size=0xF000 -o ex1.elf ex1.cpp add.cpp

The command compiles the modules ex1.cpp and add.cpp. The compiled modules are linked with the compiler libraries for C++, a heap is provided, and the executable file ex1.elf is created.

Note: Use the xc32-g++ (as opposed to the xc32-gcc driver) in order to link the project with the C++ support libraries necessary for the C++ source file in the project.