Skip to content

Compilation and Make

Jorge Pérez edited this page Mar 3, 2025 · 10 revisions

TL;DR

  • The compiler is the tool that processes your high level code, and converts it into something your can run.
  • The output of the compiler is specific to your computer configuration.
  • Make uses the Makefile config to compile code. meme8_No-compilation

How compilation works in Linux systems: https://learning.oreilly.com/library/view/how-linux-works/9781098128913/c16.xhtml#h1-500402c16-0002


Compilation process

  1. Preprocessor
  • Substitudes Macros
  • Checks for syntax errors
  1. Compiler High level code -> assembly
  2. Assembler Assembler -> executables as .o (Object) files
  3. Linker Joins .o files -> binary

https://youtu.be/2YfM-HxQd_8?si=ryGwmjAWzMQH8CRr


Linker

ExksMzwWYAUvDyl

Whats linking?

The collecting and combining various pieces of code and data into a single file that can be loaded (copied) into memory and executed. Carnegie Mellon

The linker’s job is to take all the object files that make up a program and put them together. The linker must know exactly what the memory layout of your device is so it can fit the program into memory. It’s also responsible for connecting external symbols in one file with their actual definitions in another. This process is called linking symbols.

How does this relates to libraries?

A library is a collection of object (.o) files in an archive format.

So basically the linker indicates how the assembly files should be combined to create the final executable.

The linker uses the LinkerScript.ld, where it is specified the system memory. This script contains where the Flash and RAM start, and how long are they.

  • The map file tells you where the functions in your code are stored in memory
  • The firmware file startup_stm32fxx.s (an assembly language file) contains the code that defines the ISR and specifications for other memory segments.
Section name What is it for
text. Read-only data and code
rodata Read-only data
data Initialized data
bss Data initialized to zero
COMMON Uninitialized data
.isr_vector Interrupt and reset handlers that must go in a specific place

Computer arch

Refers to how your computer components share and process data.

3-s2 0-B9780124158221000027-f02-01-9780124158221

Instruction set

CISC RISC RISC V
Complete Instruction Set Computer Reduced Instruction Set Computer Open Source
Complete set of instructions, can take more than 1 cycle Single instruction per cycle Open Source
Optimization is based on hardware Optimization is software based Open Source
Utilizes micro-transactions Compiler manages the operations Open Source
Variable instruction encoding Fixed encoding Open Source
Easily manages complex addresses Needs help when a complex address is asserted Open Source
High throughput Pipelines instructions Open Source
AMD and Intel? ARM owned Open Source

Bus of data

Von Neumann

Used on microprocessors. What your probably using 😀 .

The CPU is made out of ALU, Registes, Control Unit and a memory unit. The CPU communicates with all the components through a single bus, which can cause bandwidth limitations.

Hardvard

Used on microcontrolles/mcus. Like an Arduino.

Has different busses for reading data memory and program memory. Effectively reducing the CPU terminals to access info.

Word size

Difference-Between-8-bit-16-bit-and-32-bit-Microcontrollers

Refers to the number of bits your CPU can process at a time. This directly affects the size of your variables, and how many CPU cycles takes to process them.

Until the recent introduction of ARM CPUs, most computers just had x86 (compatible with x64 and x32 applications). On the other hand, embedded applications have always varied on this characteristic. There exist 8, 16, 32 and 64 bit processors, mainly because of cost and size restrains. However, the trend seems to support the Arm's 32 and 64 bit for the long run.

Cross compilation

EEgot3pXsAAQyro

Cross-compilation: The process of compiling for a different platform that the one that is being built in.

  1. Build system: Where code is (usually written) and built.
  2. Host system: Where code is executed.

Remember, we learn to understand the memes

dshevrhz58w41 EiKXnHSWsAIBlGJ


Using make

Running for compile

By default, make will build whats specified at first, this is the defacto target.

make

If you wish to compile any other target, then do:

make targetName

The syntax is like:

targetName: dependency1
    compiler -flags [args...]

Note: Targets may also be defined as dependencies, thus calling a compilation chain

FYI: Make can also execute bash commands, so that's why you'll sometimes see commands being used in the Makefile.

In fact the syntax, for declaring, using, and even storing variables is the same!

2fanb9


Sources on where to learn more:


CMake Basic steps:

  1. Create a sub directory with the name you'll identify the program.
  2. Create a CMakeLists.txt file whiting the sub directory.
  3. On the document, add the following lines:
    1. cmake_minimum_required(VERSION 3.5.1)

    2. project(hello)

    3. add_executable(hello hello.cpp)

    4. set(CMAKE_SYSTEM_NAME Linux)

    5. set(CMAKE_SYSTEM_PROCESSOR arm)

    6. SET(CMAKE_CXX_FLAGS "--std=c++11")

    7. set(CMAKE_C_COMPILER /usr/bin/arm-linux-gnueabi-gcc)

    8. set(CMAKE_CXX_COMPILER /usr/bin/arm-linux-gnueabi-g++)

    9. set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)

    10. set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)

    11. set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

    12. set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)

  4. Save
  5. Run CMake:
    1. mkdir build && cd build && cmake ..
  6. Build the application:
    1. make

Comments: The suggested way to cross compile code is by using toolchains. These pre-define the necessary rules needed for the platform.

Clone this wiki locally