Difference between revisions of "Toolchains, Compilers And Makefiles"

From UCT EE Wiki
Jump to navigation Jump to search
(Created page with "== Toolchains == A toolchain is a collection of tools that, in this context enables you to write code for an embedded system. For C-based development, a toolchain may consist...")
 
Line 1: Line 1:
 +
[[Category:Software]]
 
== Toolchains ==
 
== Toolchains ==
  

Revision as of 10:02, 6 February 2020

Toolchains

A toolchain is a collection of tools that, in this context enables you to write code for an embedded system. For C-based development, a toolchain may consist of the following:

A text editor or IDE
This is used to write the code that you plan to run on your embedded system.
Make
An automation tool for compiling, linking, and executing files. More on this later.
Compiler
Turns the C code you’ve written into assembly
Assembler
Turns assembly code into binary object files
Linker
A linker takes one or more object files and converts them into an executable which can run on the target system.

Usually the compiler, assembler and linker are all integrated into one single command which can be run. The most common of these is GCC (GNU Compiler Compiler Collection).

Compilation

If you are on a Linux based system and you wish to compile something, you can run:

$ g++ <file>.c -o <compiled_file_name>

Make Files

https://www.gnu.org/software/make/manual/make.html
Make files are a way of simplifying the compilation and build process.

Here’s a simplified generic makefile:

.RECIPEPREFIX += # Tells make that we are using spaces instead of tabs
CC = g++  # CC sets the compiler we’re using
CFLAGS = -lm -lrt # Set compiler flags

PROG = bin/* # Directory containing binaries to run
OBJS = obj/* # Directory containing object files


default:  # Define the default rule, called when simply running "make" in terminal
    $(CC) $(INCLUDE) $(CFLAGS) -c src/Prac2.c -o obj/Prac2.o # Compile object files
    $(CC) $(INCLUDE) $(CFLAGS) -c Tools/Timer.cpp -o obj/Timer.o # Compile library files
    $(CC) -o bin/Prac2 obj/Prac2.o obj/Timer.o $(CFLAGS) # Link object files into binary

run: # Define a new rule to be called when running "make run" in terminal
    bin/Prac2 # Run the Prac 2 binary

clean: # Define a new rule to be called when running "make clean" in terminal
    rm -rf $(PROG) $(OBJS) # Remove the compiled binaries and object files 

Cross Compilation

When working with large programs for a system, or when writing programs for an embedded system, it may be useful to use a more powerful system such as a desktop machine to compile the program. This can save you time and effort.

JetBrains CLion

Thankfully, cross-compilation is a common task and companies know this, so they develop tools to make our lives easier (and make themselves money). In the interest of your education (and the hopes that you spend money on their tools at a later stage), they make these tools accessible to you. For instructions on how to install, configure and use CLion, see CLion.