6.3.1 Single-Step C++ Compilation
A single command-line can be used to compile one file or multiple files.
Compiling a Single File
This section demonstrates how to compile and link a single file. For the purpose of this discussion, it is assumed the compiler is installed in the standard directory location and that your PATH or other environment variables are set up in such a way that the full compiler path need not be specified when you run the compiler.
The following is a basic C++ program that adds two numbers and prints the result.
Create the following program with any text editor and save it as
ex1.cpp.
#include <xc.h>
#include <cstdio>
// anonymous namespace: internal linkage, replaces 'static' in C
namespace {
template<typename T> // template: single Add works for any numeric type
// constexpr: evaluated at compile time when possible
constexpr T Add(T a, T b) { return a + b; }
}
// verified at compile time, zero runtime cost
static_assert(Add(2, 5) == 7, "Add<int> compile-time check failed");
int main()
{
// lambda: anonymous inline function stored in 'auto'
auto print = [](const char* label, double v) {
std::printf("%s %.2f\n", label, v);
};
print("int: ", Add(2, 5)); // template instantiated for int
print("float: ", Add(1.5f, 2.25f)); // template instantiated for float
print("double:", Add(1.1, 2.2));
return 0;
}
The first line of the program includes the header file
xc.h, which will include the appropriate header files that provides
definitions for all special function registers on the target device. For more
information on header files, see the 7.2 Device Header Files section.
The second line includes the library file cstdio that is the standardized
wrapper for the traditional C language header (<stdio.h>) in C++.
Compile the program by typing the following at the prompt in your favorite terminal.
xc-dsc-g++ -mcpu=33EP32MC504
-T p33EP32MC504.gld -o ex1.elf ex1.cpp
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 loaded into the MPLAB X IDE or MPLAB for VS Code.
If a hex file is required, for example, to load into a device programmer, then use the following command:
xc-dsc-bin2hex
ex1.elf
This creates an Intel hex file named ex1.hex.
Compiling Multiple Files
Move some of the code in ex1.cpp into two additional
files ex2.cpp and add.h to demonstrate the use of
multiple files in an application. That is:
/* File 1 */
/* ex1.cpp */
#include <xc.h>
#include <cstdio>
#include "add.h"
int main()
{
auto print = [](const char* label, double v) { // lambda: anonymous inline function stored in 'auto'
std::printf("%s %.2f\n", label, v);
};
print("int: ", Add(2, 5)); // template instantiated for int
print("float: ", Add(1.5f, 2.25f)); // template instantiated for float
print("double:", Add(1.1, 2.2)); // template instantiated for double
return 0;
}
/* File 2 */
/* ex2.cpp */
#include "add.h"
static_assert(Add(2, 5) == 7, "Add<int> compile-time check failed");
// verified at compile time, zero runtime cost
/* File 3 */
/* add.h */
#pragma once
namespace { // anonymous namespace: internal linkage, replaces 'static' in C
template<typename T> // template: single Add works for any numeric type
constexpr T Add(T a, T b) { return a + b; } // constexpr: evaluated at compile time when possible
}
Compile both CPP files in the one command by typing the following in your terminal program.
xc-dsc-g++ -mcpu=33EP32MC504
-T p33EP32MC504.gld -o ex1.elf ex1.cpp ex2.cpp
This command compiles the modules ex1.cpp and
ex2.cpp. The compiled modules are linked with the compiler
libraries and the executable file ex1.elf is created.
