Using LDC
Generally, LDC should be straightforward to use, like any other compiler. It comes with two different »drivers«, i.e. executables a user invokes to compile D code:
- ldc2, the main executable.
- ldmd2, a little wrapper which tries to accurately replicate the DMD command-line interface and can be used as a drop-in replacement for DMD.
Unlike ldc2, ldmd2 reads the DFLAGS environment variable and appends those flags to the underlying ldc2 cmdline (use -vdmd to check the ldc2 cmdline).
ldmd2 forwards unknown command-line options to ldc2, so advanced LDC-specific command-line options can be used with ldmd2 too.
ldc2 command-line options
- Use
ldc2 -h
for the list of basic options. - Use
ldc2 -help-hidden
for the huge list of all available options, most of which are advanced LLVM configuration options for fine-tuning. - Optimized release builds usually don't need much more than
-O -release
.- Use
-mattr
or-mcpu
to enable advanced instructions and exploit wider SIMD registers. To tailor to the host CPU, use-mcpu=native
. - Use
-flto=<thin,full>
to enable Link-Time Optimization. Add-defaultlib=phobos2-ldc-lto,druntime-ldc-lto
to include LTO-able druntime and Phobos.
When generating static LTO libraries, we recommend compiling with-flto=thin
. The generated bitcode library can then be used with both-flto=thin
and-flto=full
when generating the final binary. - Profile-Guided Optimization is also available.
- Use
Configuration file
LDC looks for an ldc2.conf file in the following directories:
- current working directory
- next to ldc2 executable
- ~/.ldc (Windows: %APPDATA%\.ldc)
- Windows only: %APPDATA%
- in the etc directory next to the directory containing the ldc2 executable
- non-Windows: <install-prefix>/etc
- non-Windows: <install-prefix>/etc/ldc
- non-Windows: /etc
- non-Windows: /etc/ldc
The file should be self-descriptive. Besides tweaking default cmdline options, its main usage is to set up cross-compilation, see Cross-compiling with LDC.
Linking
For most targets, LDC depends on a suitable installed C toolchain (e.g., gcc or clang) for linking executables and shared libraries, because druntime and Phobos build on top of a C runtime (glibc, musl, Visual C++, Bionic, …).
Windows is an exception, where LDC provides built-in linking via -link-internally and ships with custom WinSDK and MSVC import libraries (for convenience). A Visual C++ installation is still recommended though (e.g., for static linking, to prevent a MSVC runtime dependency of generated binaries).
For Posix targets, the default linker driver is the C compiler, cc (or the one specified in the CC environment variable). Name/path can be explicitly specified via the -gcc cmdline option.
For troubleshooting linking issues, you can add -v to the ldc2 cmdline to have it output the invoked linker/cc cmdline.