Ground Embedded KO Subscribe

Notes / C Language

The build process: from source to firmware

The flow through preprocessing, compiling, assembling and linking into an executable, and how to read link errors.

GROUND EMBEDDED · NOTEC Language 12The build process 1volatile uint32_t *ODR 2 = (uint32_t*)0x40020014; 3*ODR |= (1u << 5); 4while (1) { 5 if (flag) toggle(); 6}

Behind one press of the build button there are four stages.

Stage Input → output What it does
Preprocess .c → .i Expands includes and defines
Compile .i → .s C to assembly
Assemble .s → .o Assembly to a machine-code object file
Link several .o → .elf Fixes function and variable addresses and merges everything

Extracting the needed parts of the .elf gives the .bin or .hex that is programmed into the MCU.

Telling which stage an error came from

  • undeclared identifier: compile stage. A missing header or a typo
  • undefined reference to 'foo': link stage. Declared, but the .c file with the definition is missing from the build
  • multiple definition of 'x': link stage. A variable defined in a header ended up in several files
  • region FLASH overflowed: link stage. The code is larger than Flash

The map file

The .map file the linker leaves behind shows which address each function and variable was placed at and how many bytes it takes. It is the first file to open when Flash or RAM runs short.

FREE PDFGet the 3 free ebooksIf you want to keep reading this topic as one book: BLDC motor control, serial communication and sensors at book length. PDFs are in Korean.Get them free →