Skip to content
This repository has been archived by the owner on Jan 7, 2023. It is now read-only.

Compiling with DUB and LDC

Dylan Graham edited this page Oct 6, 2021 · 4 revisions

This page will show you how to compile LWDR with your project for an ARM Cortex-M microcontroller. The steps for other microcontrollers should be similar. This tutorial assumes you have some familiarity with DUB.

Before starting, here is some recommended reading:

  1. DUB Getting Started
  2. DUB Command Line Interface
  3. DUB JSON File Format
  4. LDC Wiki Page

Step 1

Create a new dub project and add LWDR to it.

Step 2 (DEBUG BUILD)

Add the following dflags to your dub.json or sdl:

"dflags": [
    "-mtriple=arm-none-eabi",
    "-mcpu=cortex-m4",
    "-g",
    "-float-abi=hard",
    "-O0",
    "-fthread-model=local-exec"
],

For an STM32F407, which is a Cortex-M4, we must have the -mtriple=arm-none-eabi to tell LDC that we are compiling for an ARM chip with no OS. -mcpu=cortex-m4 tells LDC which exact CPU we are using. -g forces LDC to compile your project (and LWDR) with debug symbols. If your debugger does not support D, you may need to use -gc instead, but the debugging capability will be less advanced. -float-abi=hard notifies LDC that our CPU has hardware floating point support. -O0 tells LDC not to optimise our code (for debugging). If you are using TLS variables, you must include the -fthread-model=local-exec flag so that LDC outputs TLS code that LWDR can work with.

Note: -g full debugging capabilities are confirmed working on the GDB debugger.

Step 2 (RELEASE BUILD)

Add the following dflags to your dub.json or sdl:

"dflags": [
    "-mtriple=arm-none-eabi",
    "-mcpu=cortex-m4",
    "-float-abi=hard",
    "-O3",
    "-fthread-model=local-exec"
],

For an STM32F407, which is a Cortex-M4, we must have the -mtriple=arm-none-eabi to tell LDC that we are compiling for an ARM chip with no OS. -mcpu=cortex-m4 tells LDC which exact CPU we are using.-float-abi=hard notifies LDC that our CPU has hardware floating point support. -O3 tells LDC to optimise your code and LWDR. If you are using TLS variables, you must include the -fthread-model=local-exec flag so that LDC outputs TLS code that LWDR can work with.

Step 3

Set your project as a static library. In your dub.json:

"targetType": "staticLibrary"

Step 4 (DEBUG BUILD)

Compile with dub build --build=debug --compiler=ldc2. This should generate a .a or .lib file which you can link against.

Step 4 (RELEASE BUILD)

Compile with dub build --build=release --compiler=ldc2. This should generate a .a or .lib file which you can link against.