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

ldc [options] files...

PARAMETERS

-o <file>
    Specifies the name of the output executable or object file.

-c
    Compiles source files into object files without linking.

-S
    Emits LLVM IR or assembly code instead of object files.

-g
    Generates debug information for use with debuggers like GDB or LLDB.

-O[level]
    Sets the optimization level, e.g., -O (default), -O1, -O2, -O3, -Os (optimize for size).

-I<dir>
    Adds a directory to the list of import paths for D modules.

-L<dir>
    Adds a directory to the list of library search paths for the linker.

-l<lib>
    Links with the specified library.

--version
    Displays the LDC compiler version and associated LLVM version.

--help
    Shows a summary of command-line options.

-unittest
    Compiles and runs unit tests defined in the source files.

-conf <file>
    Specifies an alternative path to the LDC configuration file.

-check=<arg>
    Enables runtime checks like assert, bounds, null, etc.

DESCRIPTION

LDC is a highly optimizing, native D programming language compiler that leverages the LLVM toolchain. It aims to provide fast compilation and generate efficient machine code, making it a robust choice for D developers. As an alternative to DMD (Digital Mars D Compiler) and GDC (GCC D Compiler), LDC stands out by utilizing LLVM's extensive optimization passes and support for multiple architectures. It supports various D language features, including advanced metaprogramming, concurrency, and contract programming. LDC integrates well with existing C/C++ libraries and toolchains, offering seamless interoperability. Its active development ensures compatibility with the latest D language specifications and LLVM releases.

CAVEATS

LDC's performance and behavior are inherently tied to the underlying LLVM version it utilizes.
While highly compatible with the D language specification, it might have ABI (Application Binary Interface) differences compared to other D compilers like GDC, potentially causing issues when mixing object files or libraries compiled by different D compilers.
It requires appropriate D runtime libraries to be available for linking compiled programs.

CONFIGURATION FILE

LDC uses a configuration file, typically ldc.conf, to specify default settings such as import paths, library paths, and linker options. This file's location can vary by system, but LDC usually searches in common system-wide and user-specific configuration directories. The -conf option can override this default location.

CROSS-COMPILATION

LDC supports cross-compilation, allowing developers to compile D code for different target architectures (e.g., ARM, x86_64) from a single development machine. This is achieved by specifying a target triple, like -march=x86_64 -mtriple=x86_64-linux-gnu, a feature inherited from its LLVM backend.

HISTORY

LDC began as an independent project aiming to leverage the LLVM toolchain for the D programming language. Its inception was driven by the desire to bring LLVM's advanced optimizations and cross-platform capabilities to D, offering an alternative to the official DMD compiler which had its own backend.
Since its initial development, LDC has grown into a mature and widely-used compiler in the D ecosystem, known for its high-performance code generation and strong adherence to the D language specification. It is actively maintained and continuously updated to support new D features and LLVM versions, making it a key component for many D projects.

SEE ALSO

dmd(1), gdc(1), clang(1), gcc(1), llvm(1)

Copied to clipboard