1 \input texinfo @c -*-texinfo-*-
3 @settitle The GNU D Compiler
5 @c Merge the standard indexes into a single one.
12 @include gcc-common.texi
14 @c Copyright years for this manual.
15 @set copyrights-d 2006-2022
18 @c man begin COPYRIGHT
19 Copyright @copyright{} @value{copyrights-d} Free Software Foundation, Inc.
21 Permission is granted to copy, distribute and/or modify this document
22 under the terms of the GNU Free Documentation License, Version 1.3 or
23 any later version published by the Free Software Foundation; with no
24 Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
25 A copy of the license is included in the
27 section entitled ``GNU Free Documentation License''.
29 @c man begin COPYRIGHT
37 @dircategory Software development
39 * gdc: (gdc). A GCC-based compiler for the D language
47 @title The GNU D Compiler
49 @author David Friedman, Iain Buclaw
52 @vskip 0pt plus 1filll
53 Published by the Free Software Foundation @*
54 51 Franklin Street, Fifth Floor@*
55 Boston, MA 02110-1301, USA@*
65 This manual describes how to use @command{gdc}, the GNU compiler for
66 the D programming language. This manual is specifically about
67 @command{gdc}. For more information about the D programming
68 language in general, including language specifications and standard
69 package documentation, see @uref{https://dlang.org/}.
72 * Copying:: The GNU General Public License.
73 * GNU Free Documentation License::
74 How you can share and copy this manual.
75 * Invoking gdc:: How to run gdc.
88 @c man title gdc A GCC-based compiler for the D language
91 @c man begin SYNOPSIS gdc
92 gdc [@option{-c}|@option{-S}] [@option{-g}] [@option{-pg}]
93 [@option{-O}@var{level}] [@option{-W}@var{warn}@dots{}]
94 [@option{-I}@var{dir}@dots{}] [@option{-L}@var{dir}@dots{}]
95 [@option{-f}@var{option}@dots{}] [@option{-m}@var{machine-option}@dots{}]
96 [@option{-o} @var{outfile}] [@@@var{file}] @var{infile}@dots{}
98 Only the most useful options are listed here; see below for the
102 gpl(7), gfdl(7), fsf-funding(7), gcc(1)
103 and the Info entries for @file{gdc} and @file{gcc}.
107 @c man begin DESCRIPTION gdc
109 The @command{gdc} command is the GNU compiler for the D language and
110 supports many of the same options as @command{gcc}. @xref{Option Summary, ,
111 Option Summary, gcc, Using the GNU Compiler Collection (GCC)}.
112 This manual only documents the options specific to @command{gdc}.
117 * Input and Output files:: Controlling the kind of output:
118 an executable, object files, assembler files,
119 * Runtime Options:: Options controlling runtime behavior
120 * Directory Options:: Where to find module files
121 * Code Generation:: Options controlling the output of gdc
122 * Warnings:: Options controlling warnings specific to gdc
123 * Linking:: Options influencing the linking step
124 * Developer Options:: Options useful for developers of gdc
129 @node Input and Output files
130 @section Input and Output files
131 @cindex suffixes for D source
132 @cindex D source file suffixes
134 For any given input file, the file name suffix determines what kind of
135 compilation is done. The following kinds of input file names are supported:
146 You can specify more than one input file on the @command{gdc} command line,
147 each being compiled separately in the compilation process. If you specify a
148 @code{-o @var{file}} option, all the input files are compiled together,
149 producing a single output file, named @var{file}. This is allowed even
150 when using @code{-S} or @code{-c}.
152 @cindex D interface files.
153 A D interface file contains only what an import of the module needs,
154 rather than the whole implementation of that module. They can be created
155 by @command{gdc} from a D source file by using the @code{-H} option.
156 When the compiler resolves an import declaration, it searches for matching
157 @file{.di} files first, then for @file{.d}.
159 @cindex Ddoc source files.
160 A Ddoc source file contains code in the D macro processor language. It is
161 primarily designed for use in producing user documentation from embedded
162 comments, with a slight affinity towards HTML generation. If a @file{.d}
163 source file starts with the string @code{Ddoc} then it is treated as general
164 purpose documentation, not as a D source file.
166 @node Runtime Options
167 @section Runtime Options
168 @cindex options, runtime
170 These options affect the runtime behavior of programs compiled with
175 @item -fall-instantiations
176 @cindex @option{-fall-instantiations}
177 @cindex @option{-fno-all-instantiations}
178 Generate code for all template instantiations. The default template emission
179 strategy is to not generate code for declarations that were either
180 instantiated speculatively, such as from @code{__traits(compiles, ...)}, or
181 that come from an imported module not being compiled.
184 @cindex @option{-fassert}
185 @cindex @option{-fno-assert}
186 Turn off code generation for @code{assert} contracts.
188 @item -fno-bounds-check
189 @cindex @option{-fbounds-check}
190 @cindex @option{-fno-bounds-check}
191 Turns off array bounds checking for all functions, which can improve
192 performance for code that uses arrays extensively. Note that this
193 can result in unpredictable behavior if the code in question actually
194 does violate array bounds constraints. It is safe to use this option
195 if you are sure that your code never throws a @code{RangeError}.
197 @item -fbounds-check=@var{value}
198 @cindex @option{-fbounds-check=}
199 An alternative to @option{-fbounds-check} that allows more control
200 as to where bounds checking is turned on or off. The following values
205 Turns on array bounds checking for all functions.
207 Turns on array bounds checking only for @code{@@safe} functions.
209 Turns off array bounds checking completely.
213 @cindex @option{-fbuiltin}
214 @cindex @option{-fno-builtin}
215 Don't recognize built-in functions unless they begin with the prefix
216 @samp{__builtin_}. By default, the compiler will recognize when a
217 function in the @code{core.stdc} package is a built-in function.
219 @item -fcheckaction=@var{value}
220 @cindex @option{-fcheckaction}
221 This option controls what code is generated on an assertion, bounds check, or
222 final switch failure. The following values are supported:
226 Throw an @code{AssertError} with extra context information.
228 Halt the program execution.
230 Throw an @code{AssertError} (the default).
234 @item -fdebug=@var{value}
235 @cindex @option{-fdebug}
236 @cindex @option{-fno-debug}
237 Turn on compilation of conditional @code{debug} code into the program.
238 The @option{-fdebug} option itself sets the debug level to @code{1},
239 while @option{-fdebug=} enables @code{debug} code that are identified
240 by any of the following values:
244 Sets the debug level to @var{level}, any @code{debug} code <= @var{level}
245 is compiled into the program.
247 Turns on compilation of any @code{debug} code identified by @var{ident}.
251 @cindex @option{-fdruntime}
252 @cindex @option{-fno-druntime}
253 Implements @uref{https://dlang.org/spec/betterc.html}. Assumes that
254 compilation targets an environment without a D runtime library.
256 This is equivalent to compiling with the following options:
259 gdc -nophoboslib -fno-exceptions -fno-moduleinfo -fno-rtti
262 @item -fextern-std=@var{standard}
263 @cindex @option{-fextern-std}
264 Sets the C++ name mangling compatibility to the version identified by
265 @var{standard}. The following values are supported:
270 Sets @code{__traits(getTargetInfo, "cppStd")} to @code{199711}.
272 Sets @code{__traits(getTargetInfo, "cppStd")} to @code{201103}.
274 Sets @code{__traits(getTargetInfo, "cppStd")} to @code{201402}.
276 Sets @code{__traits(getTargetInfo, "cppStd")} to @code{201703}.
279 Sets @code{__traits(getTargetInfo, "cppStd")} to @code{202002}.
282 @item -fno-invariants
283 @cindex @option{-finvariants}
284 @cindex @option{-fno-invariants}
285 Turns off code generation for class @code{invariant} contracts.
288 @cindex @option{-fmain}
289 Generates a default @code{main()} function when compiling. This is useful when
290 unittesting a library, as it enables running the unittests in a library without
291 having to manually define an entry-point function. This option does nothing
292 when @code{main} is already defined in user code.
294 @item -fno-moduleinfo
295 @cindex @option{-fmoduleinfo}
296 @cindex @option{-fno-moduleinfo}
297 Turns off generation of the @code{ModuleInfo} and related functions
298 that would become unreferenced without it, which may allow linking
299 to programs not written in D. Functions that are not be generated
300 include module constructors and destructors (@code{static this} and
301 @code{static ~this}), @code{unittest} code, and @code{DSO} registry
302 functions for dynamically linked code.
304 @item -fonly=@var{filename}
305 @cindex @option{-fonly}
306 Tells the compiler to parse and run semantic analysis on all modules
307 on the command line, but only generate code for the module specified
310 @item -fno-postconditions
311 @cindex @option{-fpostconditions}
312 @cindex @option{-fno-postconditions}
313 Turns off code generation for postcondition @code{out} contracts.
315 @item -fno-preconditions
316 @cindex @option{-fpreconditions}
317 @cindex @option{-fno-preconditions}
318 Turns off code generation for precondition @code{in} contracts.
320 @item -fpreview=@var{id}
321 @cindex @option{-fpreview}
322 Turns on an upcoming D language change identified by @var{id}. The following
323 values are supported:
327 Turns on all upcoming D language features.
329 Implements @uref{https://wiki.dlang.org/DIP1000} (Scoped pointers).
331 Implements @uref{https://wiki.dlang.org/DIP1008} (Allow exceptions in
334 Implements @uref{https://wiki.dlang.org/DIP1021} (Mutable function arguments).
336 Implements @uref{https://wiki.dlang.org/DIP25} (Sealed references).
338 Turns on generation for destructing fields of partially constructed objects.
340 Turns on generation of struct equality to use field-wise comparisons.
342 Implements new lookup rules that check the current scope for @code{alias this}
343 before searching in upper scopes.
345 Implements @code{in} parameters to mean @code{scope const [ref]} and accepts
347 @item inclusiveincontracts
348 Implements @code{in} contracts of overridden methods to be a superset of parent
351 Implements C-style integral promotion for unary @code{+}, @code{-} and @code{~}
354 Turns off and disallows all access to shared memory objects.
356 Implements rvalue arguments to @code{ref} parameters.
357 @item shortenedmethods
358 Implements use of @code{=>} for methods and top-level functions in addition to
363 @cindex @option{-frelease}
364 @cindex @option{-fno-release}
365 Turns on compiling in release mode, which means not emitting runtime
366 checks for contracts and asserts. Array bounds checking is not done
367 for @code{@@system} and @code{@@trusted} functions, and assertion
368 failures are undefined behavior.
370 This is equivalent to compiling with the following options:
373 gdc -fno-assert -fbounds-check=safe -fno-invariants \
374 -fno-postconditions -fno-preconditions -fno-switch-errors
378 @cindex @option{-frevert}
379 Turns off a D language feature identified by @var{id}. The following values
384 Turns off all revertable D language features.
386 Reverts @uref{https://wiki.dlang.org/DIP25} (Sealed references).
388 Turns off generation for destructing fields of partially constructed objects.
390 Turns off Markdown replacements in Ddoc comments.
394 @cindex @option{-frtti}
395 @cindex @option{-fno-rtti}
396 Turns off generation of run-time type information for all user defined types.
397 Any code that uses features of the language that require access to this
398 information will result in an error.
400 @item -fno-switch-errors
401 @cindex @option{-fswitch-errors}
402 @cindex @option{-fno-switch-errors}
403 This option controls what code is generated when no case is matched
404 in a @code{final switch} statement. The default run time behavior
405 is to throw a @code{SwitchError}. Turning off @option{-fswitch-errors}
406 means that instead the execution of the program is immediately halted.
409 @cindex @option{-funittest}
410 @cindex @option{-fno-unittest}
411 Turns on compilation of @code{unittest} code, and turns on the
412 @code{version(unittest)} identifier. This implies @option{-fassert}.
414 @item -fversion=@var{value}
415 @cindex @option{-fversion}
416 Turns on compilation of conditional @code{version} code into the program
417 identified by any of the following values:
421 Sets the version level to @var{level}, any @code{version} code >= @var{level}
422 is compiled into the program.
424 Turns on compilation of @code{version} code identified by @var{ident}.
427 @item -fno-weak-templates
428 @cindex @option{-fweak-templates}
429 @cindex @option{-fno-weak-templates}
430 Turns off emission of declarations that can be defined in multiple objects as
431 weak symbols. The default is to emit all public symbols as weak, unless the
432 target lacks support for weak symbols. Disabling this option means that common
433 symbols are instead put in COMDAT or become private.
437 @node Directory Options
438 @section Options for Directory Search
439 @cindex directory options
440 @cindex options, directory search
443 These options specify directories to search for files, libraries, and
444 other parts of the compiler:
450 Specify a directory to use when searching for imported modules at
451 compile time. Multiple @option{-I} options can be used, and the
452 paths are searched in the same order.
456 Specify a directory to use when searching for files in string imports
457 at compile time. This switch is required in order to use
458 @code{import(file)} expressions. Multiple @option{-J} options can be
459 used, and the paths are searched in the same order.
463 When linking, specify a library search directory, as with @command{gcc}.
467 This option specifies where to find the executables, libraries,
468 source files, and data files of the compiler itself, as with @command{gcc}.
470 @item -fmodule-file=@var{module}=@var{spec}
471 @cindex @option{-fmodule-file}
472 This option manipulates file paths of imported modules, such that if an
473 imported module matches all or the leftmost part of @var{module}, the file
474 path in @var{spec} is used as the location to search for D sources.
475 This is used when the source file path and names are not the same as the
476 package and module hierarchy. Consider the following examples:
479 gdc test.d -fmodule-file=A.B=foo.d -fmodule-file=C=bar
482 This will tell the compiler to search in all import paths for the source
483 file @var{foo.d} when importing @var{A.B}, and the directory @var{bar/}
484 when importing @var{C}, as annotated in the following D code:
488 import A.B; // Matches A.B, searches for foo.d
489 import C.D.E; // Matches C, searches for bar/D/E.d
490 import A.B.C; // No match, searches for A/B/C.d
493 @item -imultilib @var{dir}
494 @cindex @option{-imultilib}
495 Use @var{dir} as a subdirectory of the gcc directory containing
496 target-specific D sources and interfaces.
498 @item -iprefix @var{prefix}
499 @cindex @option{-iprefix}
500 Specify @var{prefix} as the prefix for the gcc directory containing
501 target-specific D sources and interfaces. If the @var{prefix} represents
502 a directory, you should include the final @code{'/'}.
505 @cindex @option{-nostdinc}
506 Do not search the standard system directories for D source and interface
507 files. Only the directories that have been specified with @option{-I} options
508 (and the directory of the current file, if appropriate) are searched.
512 @node Code Generation
513 @section Code Generation
514 @cindex options, code generation
516 In addition to the many @command{gcc} options controlling code generation,
517 @command{gdc} has several options specific to itself.
523 Generates D interface files for all modules being compiled. The compiler
524 determines the output file based on the name of the input file, removes
525 any directory components and suffix, and applies the @file{.di} suffix.
529 Same as @option{-H}, but writes interface files to directory @var{dir}.
530 This option can be used with @option{-Hf @var{file}} to independently set the
531 output file and directory path.
535 Same as @option{-H} but writes interface files to @var{file}. This option can
536 be used with @option{-Hd @var{dir}} to independently set the output file and
541 Output the module dependencies of all source files being compiled in a
542 format suitable for @command{make}. The compiler outputs one
543 @command{make} rule containing the object file name for that source file,
544 a colon, and the names of all imported files.
548 Like @option{-M} but does not mention imported modules from the D standard
549 library package directories.
553 When used with @option{-M} or @option{-MM}, specifies a @var{file} to write
554 the dependencies to. When used with the driver options @option{-MD} or
555 @option{-MMD}, @option{-MF} overrides the default dependency output file.
559 This option is for compatibility with @command{gcc}, and is ignored by the
564 Outputs a phony target for each dependency other than the modules being
565 compiled, causing each to depend on nothing.
567 @item -MT @var{target}
569 Change the @var{target} of the rule emitted by dependency generation
570 to be exactly the string you specify. If you want multiple targets,
571 you can specify them as a single argument to @option{-MT}, or use
572 multiple @option{-MT} options.
574 @item -MQ @var{target}
576 Same as @option{-MT}, but it quotes any characters which are special to
581 This option is equivalent to @option{-M -MF @var{file}}. The driver
582 determines @var{file} by removing any directory components and suffix
583 from the input file, and then adding a @file{.deps} suffix.
586 @cindex @option{-MMD}
587 Like @option{-MD} but does not mention imported modules from the D standard
588 library package directories.
592 Output information describing the contents of all source files being
593 compiled in JSON format to a file. The driver determines @var{file} by
594 removing any directory components and suffix from the input file, and then
595 adding a @file{.json} suffix.
599 Same as @option{-X}, but writes all JSON contents to the specified
603 @cindex @option{-fdoc}
604 Generates @code{Ddoc} documentation and writes it to a file. The compiler
605 determines @var{file} by removing any directory components and suffix
606 from the input file, and then adding a @file{.html} suffix.
608 @item -fdoc-dir=@var{dir}
609 @cindex @option{-fdoc-dir}
610 Same as @option{-fdoc}, but writes documentation to directory @var{dir}.
611 This option can be used with @option{-fdoc-file=@var{file}} to
612 independently set the output file and directory path.
614 @item -fdoc-file=@var{file}
615 @cindex @option{-fdoc-file}
616 Same as @option{-fdoc}, but writes documentation to @var{file}. This
617 option can be used with @option{-fdoc-dir=@var{dir}} to independently
618 set the output file and directory path.
620 @item -fdoc-inc=@var{file}
621 @cindex @option{-fdoc-inc}
622 Specify @var{file} as a @var{Ddoc} macro file to be read. Multiple
623 @option{-fdoc-inc} options can be used, and files are read and processed
626 @item -fdump-c++-spec=@var{file}
627 For D source files, generate corresponding C++ declarations in @var{file}.
629 @item -fdump-c++-spec-verbose
630 In conjunction with @option{-fdump-c++-spec=} above, add comments for ignored
631 declarations in the generated C++ header.
633 @item -fsave-mixins=@var{file}
634 @cindex @option{-fsave-mixins}
635 Generates code expanded from D @code{mixin} statements and writes the
636 processed sources to @var{file}. This is useful to debug errors in compilation
637 and provides source for debuggers to show when requested.
643 @cindex options to control warnings
644 @cindex warning messages
645 @cindex messages, warning
646 @cindex suppressing warnings
648 Warnings are diagnostic messages that report constructions that
649 are not inherently erroneous but that are risky or suggest there
650 is likely to be a bug in the program. Unless @option{-Werror} is
651 specified, they do not prevent compilation of the program.
656 @cindex @option{-Wall}
657 @cindex @option{-Wno-all}
658 Turns on all warnings messages. Warnings are not a defined part of
659 the D language, and all constructs for which this may generate a
660 warning message are valid code.
663 @cindex @option{-Walloca}
664 This option warns on all uses of "alloca" in the source.
666 @item -Walloca-larger-than=@var{n}
667 @cindex @option{-Walloca-larger-than}
668 @cindex @option{-Wno-alloca-larger-than}
669 Warn on unbounded uses of alloca, and on bounded uses of alloca
670 whose bound can be larger than @var{n} bytes.
671 @option{-Wno-alloca-larger-than} disables
672 @option{-Walloca-larger-than} warning and is equivalent to
673 @option{-Walloca-larger-than=@var{SIZE_MAX}} or larger.
676 @cindex @option{-Wcast-result}
677 @cindex @option{-Wno-cast-result}
678 Warn about casts that will produce a null or zero result. Currently
679 this is only done for casting between an imaginary and non-imaginary
680 data type, or casting between a D and C++ class.
682 @item -Wno-deprecated
683 @cindex @option{-Wdeprecated}
684 @cindex @option{-Wno-deprecated}
685 Do not warn about usage of deprecated features and symbols with
686 @code{deprecated} attributes.
689 @cindex @option{-Werror}
690 @cindex @option{-Wno-error}
691 Turns all warnings into errors.
694 @cindex @option{-Wspeculative}
695 @cindex @option{-Wno-speculative}
696 List all error messages from speculative compiles, such as
697 @code{__traits(compiles, ...)}. This option does not report
698 messages as warnings, and these messages therefore never become
699 errors when the @option{-Werror} option is also used.
702 @cindex @option{-Wtemplates}
703 @cindex @option{-Wno-templates}
704 Warn when a template instantiation is encountered. Some coding
705 rules disallow templates, and this may be used to enforce that rule.
707 @item -Wunknown-pragmas
708 @cindex @option{-Wunknown-pragmas}
709 @cindex @option{-Wno-unknown-pragmas}
710 Warn when a @code{pragma()} is encountered that is not understood by
711 @command{gdc}. This differs from @option{-fignore-unknown-pragmas}
712 where a pragma that is part of the D language, but not implemented by
713 the compiler, won't get reported.
718 Do not warn upon questionable usage of the macros used to handle variable
719 arguments like @code{va_start}.
721 @item -fignore-unknown-pragmas
722 @cindex @option{-fignore-unknown-pragmas}
723 @cindex @option{-fno-ignore-unknown-pragmas}
724 Turns off errors for unsupported pragmas.
726 @item -fmax-errors=@var{n}
727 @cindex @option{-fmax-errors}
728 Limits the maximum number of error messages to @var{n}, at which point
729 @command{gdc} bails out rather than attempting to continue processing the
730 source code. If @var{n} is 0 (the default), there is no limit on the
731 number of error messages produced.
734 @cindex @option{-fsyntax-only}
735 @cindex @option{-fno-syntax-only}
736 Check the code for syntax errors, but do not actually compile it. This
737 can be used in conjunction with @option{-fdoc} or @option{-H} to generate
738 files for each module present on the command-line, but no other output
741 @item -ftransition=@var{id}
742 @cindex @option{-ftransition}
743 Report additional information about D language changes identified by
744 @var{id}. The following values are supported:
748 List information on all D language transitions.
750 List all usages of complex or imaginary types.
752 List all non-mutable fields which occupy an object instance.
754 List all usages of @code{in} on parameter.
756 List all hidden GC allocations.
758 List statistics on template instantiations.
760 List all variables going into thread local storage.
762 List instances of Markdown replacements in Ddoc.
768 @section Options for Linking
769 @cindex options, linking
770 @cindex linking, static
772 These options come into play when the compiler links object files into an
773 executable output file. They are meaningless if the compiler is not doing
778 @item -defaultlib=@var{libname}
779 @cindex @option{-defaultlib=}
780 Specify the library to use instead of libphobos when linking. Options
781 specifying the linkage of libphobos, such as @option{-static-libphobos}
782 or @option{-shared-libphobos}, are ignored.
784 @item -debuglib=@var{libname}
785 @cindex @option{-debuglib=}
786 Specify the debug library to use instead of libphobos when linking.
787 This option has no effect unless the @option{-g} option was also given
788 on the command line. Options specifying the linkage of libphobos, such
789 as @option{-static-libphobos} or @option{-shared-libphobos}, are ignored.
792 @cindex @option{-nophoboslib}
793 Do not use the Phobos or D runtime library when linking. Options specifying
794 the linkage of libphobos, such as @option{-static-libphobos} or
795 @option{-shared-libphobos}, are ignored. The standard system libraries are
796 used normally, unless @option{-nostdlib} or @option{-nodefaultlibs} is used.
798 @item -shared-libphobos
799 @cindex @option{-shared-libphobos}
800 On systems that provide @file{libgphobos} and @file{libgdruntime} as a
801 shared and a static library, this option forces the use of the shared
802 version. If no shared version was built when the compiler was configured,
803 this option has no effect.
805 @item -static-libphobos
806 @cindex @option{-static-libphobos}
807 On systems that provide @file{libgphobos} and @file{libgdruntime} as a
808 shared and a static library, this option forces the use of the static
809 version. If no static version was built when the compiler was configured,
810 this option has no effect.
814 @node Developer Options
815 @section Developer Options
816 @cindex developer options
817 @cindex debug dump options
820 This section describes command-line options that are primarily of
821 interest to developers or language tooling.
825 @item -fdump-d-original
826 @cindex @option{-fdump-d-original}
827 Output the internal front-end AST after the @code{semantic3} stage.
828 This option is only useful for debugging the GNU D compiler itself.
832 Dump information about the compiler language processing stages as the source
833 program is being compiled. This includes listing all modules that are
834 processed through the @code{parse}, @code{semantic}, @code{semantic2}, and
835 @code{semantic3} stages; all @code{import} modules and their file paths;
836 and all @code{function} bodies that are being compiled.