CM-Makefile: Makefile template for Cortex-M projects
Makefile template for ARM Cortex-M projects using the GCC ARM Embedded toolchain. The template is meant to be included in project-specific Makefile.
See repository cm-makefile-examples for usage examples.
To successfully compile and debug the project, you need:
- Linux desktop (OS X should work as well)
- GCC ARM Embedded toolchain
- MCU vendor's HAL (start-up code, register definitions, ...)
Installation instructions are described in a separate file.
Makefile. The Makefile should link to source files outside the
project's source directory (such as libraries provided by the manufacturer) and
*.mk files from the CM-Makefile project. For example:
BIN = hello # Name of the output binary SDK_DIR = ../mcu-vendor # Target-specific library INC = -I$(SDK_DIR)/inc SRC_ASM = $(SDK_DIR)/src/startup.S SCR_C = $(SDK_DIR)/src/system.c SRC_LD = $(SDK_DIR)/target.ld OPENOCD = openocd -f board/my_board.cfg include cm-makefile/config.mk include cm-makefile/openocd.mk # For flash, debug and gdb targets include cm-makefile/rules.mk
See the cm-makefile-examples repository for real-world examples.
Then simply run
make to compile the project and
make flash to upload it to
The process will create
.bin binaries in the
directory together with other files useful for debugging:
.disasm: Disassembly output
.sym: Name list (useful to check memory layout)
.map: Linker map (useful to check linked object files, discarded sections, etc.)
To use a specific toolchain (other than the default
Flashing and debugging
To flash the program using OpenOCD, run
make flash. To reset the CPU, run
To debug the project, run
make gdb (which starts OpenOCD's GDB server)
make debug in a different terminal (which starts GDB in TUI
It is also possible to use the Eclipse standalone debugger using
make cdtdebut. The debugger has to be available as
cdtdebug. If it isn't,
simply change the
The configuration is quite straightforward. Most of variables used in
rules.mk can be modified by the project-specific Makefile.
INC: Include path for header files (
-I*.h) or linker scripts (
SRC_ASM: Assembly source files (
*.s) outside the current directory (e.g. target-specific startup code)
SRC_C: C source files (
*.c) outside the current directory
SRC_LD: Linker script (
DEF: Custom macros (
ARCHFLAGS: CPU architecture (see below)
FPFLAGS: FLoating point configuration (see below)
WARNFLAGS: GCC warning options. You can define your own set of options or disable the unwanted ones by appending
DBGFLAGS: GCC debugging options (
OPTFLAGS: GCC optimization options (
PREPFLAGS: GCC preprocessor options (
-MD -MPby defualt)
OPENOCD: OpenOCD command (with script selection) to set up debug session. You can use one of the built-in scripts or create your own.
Configuration for a different target
CM-Makefile can be easily used with a different CPU core than the default
Cortex-M0. It's only necessary to configure the
FPFLAGS defaults to
|Cortex-M4 (Soft FP)||
|Cortex-M4 (Hard FP)||
CM-Makefile is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
COPYING.LESSER for details.