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 */

// 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;

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=ATSAME70Q21B -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++ driver (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.