I tried to set up Code::Blocks to use libopencm3, which was not that hard, after all. It's just a lot of work, but it can be stored as a template! This post might appear to be very long, but about 90% of it are screenshots, and cut 'n paste code.
Sources of information
First of all I downloaded and compiled libopencm3 according to their instructions, with a custom destination directory. This left me with a set of chip libraries (opencm3_stm32f0 and many more for other chips) to be included in my project. I also have the compiler configured already. So here are a few screenshots of my C::B setup.
Create an empty project (File->New->Project):
Hit go and enter your project's title and filename. Select your arm-none-eabi-gcc when asked for a compiler, and hit finish (this automatically creates debug and release configurations):
Fine, there's now an empty project in the workspace that we can fill with our source code and build info.
Create a main.cpp (File->New->Empty File or CTRL-Shift-N) and add it to the project. Mine looks like this (based on code taken from one of the STM32F0 examples from https://github.com/libopencm3/libopencm3-examples/blob/master/examples/stm32/f0/stm32f0-discovery/systick_blink/systick_blink.c):
#define PORT_LED_ONBOARD GPIOB
#define PIN_LED_ONBOARD GPIO1
static void systick_setup(int freq)
STK_CVR = 0;
systick_set_reload(rcc_ahb_frequency / freq);
static void clock_setup(void)
static void gpio_setup(void)
gpio_mode_setup(PORT_LED_ONBOARD, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, PIN_LED_ONBOARD);
Compiler and Linker flags
Now comes the trickier part: Set up flags for the compiler and the linker. Go to Project->Build options and select the top-level entry, which has options for all sub-targets (debug and release):
My compiler flags in the "Compiler Settings" tab, combined from the tabs "Compiler Flags" and "Other options", are:
and one define in the "#defines" tab:
Now for the linker ("Linker settings" tab):
That is in the "Link libraries" list:
and in the "Other linker options":
Search Directories for the compiler:
(this requires libopencm3 to be in the project root directory)
For the linker:
The first entry in the search directory list is just a dot, so the linker will find a linker script in the project root directory. The other entry is a relative path to the pre-built libraries for all chips.
We're almost there, just some fine-tuning after the build is finished:
arm-none-eabi-objcopy -O binary $(TARGET_OUTPUT_FILE) $(TARGET_OUTPUT_DIR)$(TARGET_OUTPUT_BASENAME).bin
Now you can also add extra flags for debug and release builds (generate...
Read more »