LinuxCommandLibrary

ldc

Compile D programming language code

TLDR

Compile a source code file into an executable binary

$ ldc2 [path/to/source.d] -of=[path/to/output_executable]
copy

Compile the source code file without linking
$ ldc2 -c [path/to/source.d]
copy

Select the target architecture and OS
$ ldc -mtriple=[architecture_OS] -c [path/to/source.d]
copy

Display help
$ ldc2 -h
copy

Display complete help
$ ldc2 -help-hidden
copy

SYNOPSIS

ldc2 [options...] [-of=output] input-files.d

PARAMETERS

-o, --output=file
    Set output file name (executable or object)

-Ipath
    Add directory to import search path

-Lflag
    Pass linker flag to the linker

-release
    Enable optimizations, disable asserts and bounds checks

-debug
    Enable debug info and assertions

-O
    Set optimization level (0-4, default 2)

-gc
    Select garbage collector (none, conservative, precise)

-run
    Compile and run the program immediately

-lib
    Generate static or dynamic library

-w
    Enable warnings

-v
    Verbose output

-d
    Set version identifier

-unittest
    Enable unit tests

-main
    Generate main() function for unittesting

--help
    Display help message

DESCRIPTION

LDC (LLVM D Compiler) is a production-grade compiler for the D programming language, leveraging the LLVM infrastructure for code generation and optimization. It aims to provide the best blend of rapid compilation speed, cross-platform capabilities, and quality code generation. LDC is compatible with the Digital Mars D (DMD) frontend syntax, allowing seamless migration from DMD while offering superior performance through LLVM's optimizer passes.

Key features include full support for D2/D language features, efficient garbage collection integration, inline assembly, and extensive optimization flags inherited from LLVM (e.g., -O3, loop unrolling). It supports multiple targets like x86, ARM, PowerPC, and WebAssembly, making it ideal for systems programming, game development, and high-performance applications. LDC compiles D source files (.d) into native executables or object files, with options for linking and runtime embedding.

Unlike DMD's built-in backend, LDC produces smaller, faster binaries due to LLVM's mature optimizer. It's widely used in projects requiring low-latency compilation and portable code.

CAVEATS

Not installed by default; requires LLVM dev packages. Command is typically ldc2, not ldc. Some DMD-incompatible optimizations may alter behavior. Large projects need sufficient RAM for LLVM passes.

CONFIGURATION

Uses ldc.conf or ~/.ldc.conf for default flags, import paths, and linker settings.
Example: -I/usr/include/dmd -L-lphobos2

EXAMPLES

ldc2 hello.d -of=hello compiles to executable.
ldc2 -run hello.d compiles and runs.
ldc2 -lib -of=libfoo.a foo.d builds static library.

HISTORY

LDC originated in 2007 as an experimental LLVM backend for D by David Friedman. First official release (0.9) in 2008. Reached 1.0 in 2017, now at version 1.35+ with ongoing LLVM integration. Gained popularity for outperforming DMD in benchmarks.

SEE ALSO

dmd(1), gdc(1), llc(1), clang(1), opt(1)

Copied to clipboard