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.
LDC also comes with a few tools:
- ldc-build-runtime, a tool to recompile the D runtime and standard library to your wishes (for example for cross-compiling to a different architecture), see Building LDC runtime libraries.
ldc2 command-line options
ldc2 -hfor the list of basic options.
ldc2 -help-hiddenfor 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
-mcputo enable advanced instructions and exploit wider SIMD registers. To tailor to the host CPU, use
-flto=<thin,full>to enable Link-Time Optimization. Add
-defaultlib=phobos2-ldc-lto,druntime-ldc-ltoto 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=fullwhen generating the final binary.
- Profile-Guided Optimization is also available.
-mtripleto select the target architecture (CPU) and OS. LDC is an implicit cross-compiler, meaning that one binary of LDC can compile to many targets, see Cross-compiling with LDC.
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.
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.