3. Creating a Simple Application with the tiarmclang Compiler Tools¶
This section of the Getting Started Guide provides an example of how to build a simple application using the tiarmclang command-line interface. In addition, it provides a walk-through of how to build a simple application in a Code Composer Studio project that uses the tiarmclang compiler.
3.1. Source Files¶
The subsections below will describe how to build a simple “Hello World!” program using either the tiarmclang command-line interface or the Code Composer Studio (CCS) development environment. For the purposes of these tutorial examples, it is assumed that you have a C source file containing the following C code:
1#include <stdio.h>
2
3int main() {
4 printf("Hello World\n");
5 return 0;
6}
It is also assumed that you have at your disposal a linker command file that provides a specification of the available memory and how to place compiler/linker generated output sections in that memory. For example, in the tutorials below, the following linker command file, named lnkme.cmd, could be used:
/****************************************************************************/
/* Example Linker Command File */
/****************************************************************************/
-c /* LINK USING C CONVENTIONS */
-stack 0x8000 /* SOFTWARE STACK SIZE */
-heap 0x2000 /* HEAP AREA SIZE */
--args 0x1000
/* SPECIFY THE SYSTEM MEMORY MAP */
MEMORY
{
P_MEM : org = 0x00000020 len = 0x20000000 /* PROGRAM MEMORY (ROM) */
D_MEM : org = 0x20000020 len = 0x20000000 /* DATA MEMORY (RAM) */
}
/* SPECIFY THE SECTIONS ALLOCATION INTO MEMORY */
SECTIONS
{
.intvecs : {} > 0x0 /* INTERRUPT VECTORS */
.bss : {} > D_MEM /* GLOBAL & STATIC VARS */
.data : {} > D_MEM
.sysmem : {} > D_MEM /* DYNAMIC MEMORY ALLOCATION AREA */
.stack : {} > D_MEM /* SOFTWARE SYSTEM STACK */
.text : {} > P_MEM /* CODE */
.cinit : {} > P_MEM /* INITIALIZATION TABLES */
.const : {} > P_MEM /* CONSTANT DATA */
.rodata : {} > P_MEM
.ARM.exidx : {} > P_MEM
.init_array : {} > P_MEM /* C++ CONSTRUCTOR TABLES */
}
3.2. Compile and Link Using Command-Line¶
You can use a single command line to compile your source files and link against C/C++ runtime libraries to create an executable file. By default, the tiarmclang compiler will compile and attempt to link compiler generated object files with runtime libraries. If you only want to compile your source files into object files without linking, the tiarmclang -c option can be added to the command line.
If you were building the “Hello World!” example program to run on a TI Arm Cortex-m4 processor, you could use the following command:
%> tiarmclang -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16 hello.c -o hello_world.out -Xlinker -llnkme.cmd -Xlinker -mhello_world.map
When the tiarmclang compiler runs it will implicitly add the directories where the C/C++ runtime header files are installed to the include file directory search path. Likewise, when the linker is invoked by tiarmclang, it will implicitly add the directories where the C/C++ runtime libraries are installed to your library file directory search path. In addition, the linker will implicitly include the list of applicable C/C++ runtime libraries into a link to resolve references to C/C++ runtime and built-in functions and data objects.
The above command will produce an executable file, hello_world.out, that can be loaded and run on a TI Arm Cortex-m4 processor.
3.3. Compile and Link Using Code Composer Studio¶
If you use CCS as your development environment, the compiler and linker option are automatically set for you when you create a project. The build settings that are created when the project is created determine which compiler and linker command-line options are used to build the project and can be adjusted as needed.
To create and build the “Hello World!” example as a CCS project, follow these steps:
Create a project
1.1) Choose File > New > CCS Project from the “File” tab
1.2) In the “New CCS Project” wizard, if you are compiling for a specific TI Arm processor, then you can select the processor from the Target drop-down menus. For the purposes of this tutorial, the “Generic ARM7 Device” setting was selected in the right-hand side Target drop-down menu.
1.3) In the Project name field, type a name for the project. For the purposes of this tutorial, we’ll refer to the project name “hello_world”.
1.4) In the Compiler version drop-down menu, select the tiarmclang compiler that you have installed.
1.5) Expand the Project type and tool-chain section, then select the device endianness. For this tutorial, a little-endian device is assumed.
1.6) Expand the Project templates and examples section, then select a template for your project. For this tutorial, the “Empty Project” template is assumed.
1.7) Click Finish. A new project, “hello_world”, will be added to your current workspace.
Add source files
2.1) Left click on the “hello_world” project in the current workspace to make it the active project.
2.2) Right-click on the “hello_world” project, then select Add Files…” from the drop-down menu. You can then browse to find a C source file containing the code described in “Source Files” subsection above. When the source file is found and selected in the Add files to hello_world pop-up browser, clock on Open and then copy the file into the project. For this tutorial, the source file name is assumed to be “hello.c”.
2.3) Repeat step 2.3 to find and copy an appropriate linker command file to be used in the project. For this tutorial, a linker command file named “lnkme.cmd” is assumed.
Open and adjust project build settings as needed
3.1) Right-click on the “hello_world” project, then select Show Build Settings… or Properties from the drop-down menu.
3.2) In the Properties for hello_world pop-up dialog box, walk through the categories along the left-hand side of the dialog and make necessary adjustments in each category:
3.2.1) In the General category, check that the proper compiler version, Device endianness, and Linker command file are selected
3.2.2) In the Arm Compiler > Processor Options category, select the appropriate -march, -mcpu, -mfloat-abi, -mfpu, and arm/thumb options from each of the drop-down menus in the Processor Options window. For this tutorial, -march is set to thumbv7em, -mcpu is set to cortex-m4, -mfloat-abi is set to hard, -mfpu is set to fpv4-sp-d16, and arm/thumb is set to -mthumb.
3.2.3) Further adjustments to other categories are not necessary for the purposes of this tutorial.
3.2.4) When adjustments to the Properties for hello_world are complete, click on Apply and Close
Build the project
4.1) Right-click on the “hello_world” project, then select Build Project from the drop-down menu.
4.2) As CCS runs the compiler and linker commands, the project build progress will appear in the CCS Console window, with a resulting output file named “hello_world.out”. This .out file can then be loaded and run on an appropriate TI Arm processor. If a Cortex-m4 processor was selected for the -mcpu option, then the resulting .out file can be loaded and run on a TI Arm Cortex-m4 processor (like MSP432E4, for example).