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-2018
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{http://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.
220 @item -fdebug=@var{value}
221 @cindex @option{-fdebug}
222 @cindex @option{-fno-debug}
223 Turn on compilation of conditional @code{debug} code into the program.
224 The @option{-fdebug} option itself sets the debug level to @code{1},
225 while @option{-fdebug=} enables @code{debug} code that are identified
226 by any of the following values:
230 Sets the debug level to @var{level}, any @code{debug} code <= @var{level}
231 is compiled into the program.
233 Turns on compilation of any @code{debug} code identified by @var{ident}.
236 @item -fno-invariants
237 @cindex @option{-finvariants}
238 @cindex @option{-fno-invariants}
239 Turns off code generation for class @code{invariant} contracts.
241 @item -fno-moduleinfo
242 @cindex @option{-fmoduleinfo}
243 @cindex @option{-fno-moduleinfo}
244 Turns off generation of the @code{ModuleInfo} and related functions
245 that would become unreferenced without it, which may allow linking
246 to programs not written in D. Functions that are not be generated
247 include module constructors and destructors (@code{static this} and
248 @code{static ~this}), @code{unittest} code, and @code{DSO} registry
249 functions for dynamically linked code.
251 @item -fonly=@var{filename}
252 @cindex @option{-fonly}
253 Tells the compiler to parse and run semantic analysis on all modules
254 on the command line, but only generate code for the module specified
257 @item -fno-postconditions
258 @cindex @option{-fpostconditions}
259 @cindex @option{-fno-postconditions}
260 Turns off code generation for postcondition @code{out} contracts.
262 @item -fno-preconditions
263 @cindex @option{-fpreconditions}
264 @cindex @option{-fno-preconditions}
265 Turns off code generation for precondition @code{in} contracts.
268 @cindex @option{-frelease}
269 @cindex @option{-fno-release}
270 Turns on compiling in release mode, which means not emitting runtime
271 checks for contracts and asserts. Array bounds checking is not done
272 for @code{@@system} and @code{@@trusted} functions, and assertion
273 failures are undefined behavior.
275 This is equivalent to compiling with the following options:
278 gdc -fno-assert -fbounds-check=safe -fno-invariants \
279 -fno-postconditions -fno-preconditions -fno-switch-errors
282 @item -fno-switch-errors
283 @cindex @option{-fswitch-errors}
284 @cindex @option{-fno-switch-errors}
285 This option controls what code is generated when no case is matched
286 in a @code{final switch} statement. The default run time behavior
287 is to throw a @code{SwitchError}. Turning off @option{-fswitch-errors}
288 means that instead the execution of the program is immediately halted.
291 @cindex @option{-funittest}
292 @cindex @option{-fno-unittest}
293 Turns on compilation of @code{unittest} code, and turns on the
294 @code{version(unittest)} identifier. This implies @option{-fassert}.
296 @item -fversion=@var{value}
297 @cindex @option{-fversion}
298 Turns on compilation of conditional @code{version} code into the program
299 identified by any of the following values:
303 Sets the version level to @var{level}, any @code{version} code >= @var{level}
304 is compiled into the program.
306 Turns on compilation of @code{version} code identified by @var{ident}.
310 @cindex @option{-fweak}
311 @cindex @option{-fno-weak}
312 Turns off emission of instantiated declarations that can be defined in multiple
313 objects as weak or one-only symbols. The default is to emit all public symbols
314 as weak, unless the target lacks support for weak symbols. Disabling this
315 option means that common symbols are instead put in COMDAT or become private.
319 @node Directory Options
320 @section Options for Directory Search
321 @cindex directory options
322 @cindex options, directory search
325 These options specify directories to search for files, libraries, and
326 other parts of the compiler:
332 Specify a directory to use when searching for imported modules at
333 compile time. Multiple @option{-I} options can be used, and the
334 paths are searched in the same order.
338 Specify a directory to use when searching for files in string imports
339 at compile time. This switch is required in order to use
340 @code{import(file)} expressions. Multiple @option{-J} options can be
341 used, and the paths are searched in the same order.
345 When linking, specify a library search directory, as with @command{gcc}.
349 This option specifies where to find the executables, libraries,
350 source files, and data files of the compiler itself, as with @command{gcc}.
352 @item -fmodule-file=@var{module}=@var{spec}
353 @cindex @option{-fmodule-file}
354 This option manipulates file paths of imported modules, such that if an
355 imported module matches all or the leftmost part of @var{module}, the file
356 path in @var{spec} is used as the location to search for D sources.
357 This is used when the source file path and names are not the same as the
358 package and module hierarchy. Consider the following examples:
361 gdc test.d -fmodule-file=A.B=foo.d -fmodule-file=C=bar
364 This will tell the compiler to search in all import paths for the source
365 file @var{foo.d} when importing @var{A.B}, and the directory @var{bar/}
366 when importing @var{C}, as annotated in the following D code:
370 import A.B; // Matches A.B, searches for foo.d
371 import C.D.E; // Matches C, searches for bar/D/E.d
372 import A.B.C; // No match, searches for A/B/C.d
375 @item -imultilib @var{dir}
376 @cindex @option{-imultilib}
377 Use @var{dir} as a subdirectory of the gcc directory containing
378 target-specific D sources and interfaces.
380 @item -iprefix @var{prefix}
381 @cindex @option{-iprefix}
382 Specify @var{prefix} as the prefix for the gcc directory containing
383 target-specific D sources and interfaces. If the @var{prefix} represents
384 a directory, you should include the final @code{'/'}.
387 @cindex @option{-nostdinc}
388 Do not search the standard system directories for D source and interface
389 files. Only the directories that have been specified with @option{-I} options
390 (and the directory of the current file, if appropriate) are searched.
394 @node Code Generation
395 @section Code Generation
396 @cindex options, code generation
398 In addition to the many @command{gcc} options controlling code generation,
399 @command{gdc} has several options specific to itself.
405 Generates D interface files for all modules being compiled. The compiler
406 determines the output file based on the name of the input file, removes
407 any directory components and suffix, and applies the @file{.di} suffix.
411 Same as @option{-H}, but writes interface files to directory @var{dir}.
412 This option can be used with @option{-Hf @var{file}} to independently set the
413 output file and directory path.
417 Same as @option{-H} but writes interface files to @var{file}. This option can
418 be used with @option{-Hd @var{dir}} to independently set the output file and
423 Output the module dependencies of all source files being compiled in a
424 format suitable for @command{make}. The compiler outputs one
425 @command{make} rule containing the object file name for that source file,
426 a colon, and the names of all imported files.
430 Like @option{-M} but does not mention imported modules from the D standard
431 library package directories.
435 When used with @option{-M} or @option{-MM}, specifies a @var{file} to write
436 the dependencies to. When used with the driver options @option{-MD} or
437 @option{-MMD}, @option{-MF} overrides the default dependency output file.
441 This option is for compatibility with @command{gcc}, and is ignored by the
446 Outputs a phony target for each dependency other than the modules being
447 compiled, causing each to depend on nothing.
449 @item -MT @var{target}
451 Change the @var{target} of the rule emitted by dependency generation
452 to be exactly the string you specify. If you want multiple targets,
453 you can specify them as a single argument to @option{-MT}, or use
454 multiple @option{-MT} options.
456 @item -MQ @var{target}
458 Same as @option{-MT}, but it quotes any characters which are special to
463 This option is equivalent to @option{-M -MF @var{file}}. The driver
464 determines @var{file} by removing any directory components and suffix
465 from the input file, and then adding a @file{.deps} suffix.
468 @cindex @option{-MMD}
469 Like @option{-MD} but does not mention imported modules from the D standard
470 library package directories.
474 Output information describing the contents of all source files being
475 compiled in JSON format to a file. The driver determines @var{file} by
476 removing any directory components and suffix from the input file, and then
477 adding a @file{.json} suffix.
481 Same as @option{-X}, but writes all JSON contents to the specified
485 @cindex @option{-fdoc}
486 Generates @code{Ddoc} documentation and writes it to a file. The compiler
487 determines @var{file} by removing any directory components and suffix
488 from the input file, and then adding a @file{.html} suffix.
490 @item -fdoc-dir=@var{dir}
491 @cindex @option{-fdoc-dir}
492 Same as @option{-fdoc}, but writes documentation to directory @var{dir}.
493 This option can be used with @option{-fdoc-file=@var{file}} to
494 independently set the output file and directory path.
496 @item -fdoc-file=@var{file}
497 @cindex @option{-fdoc-file}
498 Same as @option{-fdoc}, but writes documentation to @var{file}. This
499 option can be used with @option{-fdoc-dir=@var{dir}} to independently
500 set the output file and directory path.
502 @item -fdoc-inc=@var{file}
503 @cindex @option{-fdoc-inc}
504 Specify @var{file} as a @var{Ddoc} macro file to be read. Multiple
505 @option{-fdoc-inc} options can be used, and files are read and processed
512 @cindex options to control warnings
513 @cindex warning messages
514 @cindex messages, warning
515 @cindex suppressing warnings
517 Warnings are diagnostic messages that report constructions that
518 are not inherently erroneous but that are risky or suggest there
519 is likely to be a bug in the program. Unless @option{-Werror} is
520 specified, they do not prevent compilation of the program.
525 @cindex @option{-Wall}
526 @cindex @option{-Wno-all}
527 Turns on all warnings messages. Warnings are not a defined part of
528 the D language, and all constructs for which this may generate a
529 warning message are valid code.
532 @cindex @option{-Walloca}
533 This option warns on all uses of "alloca" in the source.
535 @item -Walloca-larger-than=@var{n}
536 @cindex @option{-Walloca-larger-than}
537 @cindex @option{-Wno-alloca-larger-than}
538 Warn on unbounded uses of alloca, and on bounded uses of alloca
539 whose bound can be larger than @var{n} bytes.
540 @option{-Wno-alloca-larger-than} disables
541 @option{-Walloca-larger-than} warning and is equivalent to
542 @option{-Walloca-larger-than=@var{SIZE_MAX}} or larger.
545 @cindex @option{-Wcast-result}
546 @cindex @option{-Wno-cast-result}
547 Warn about casts that will produce a null or zero result. Currently
548 this is only done for casting between an imaginary and non-imaginary
549 data type, or casting between a D and C++ class.
551 @item -Wno-deprecated
552 @cindex @option{-Wdeprecated}
553 @cindex @option{-Wno-deprecated}
554 Do not warn about usage of deprecated features and symbols with
555 @code{deprecated} attributes.
558 @cindex @option{-Werror}
559 @cindex @option{-Wno-error}
560 Turns all warnings into errors.
563 @cindex @option{-Wspeculative}
564 @cindex @option{-Wno-speculative}
565 List all error messages from speculative compiles, such as
566 @code{__traits(compiles, ...)}. This option does not report
567 messages as warnings, and these messages therefore never become
568 errors when the @option{-Werror} option is also used.
571 @cindex @option{-Wtemplates}
572 @cindex @option{-Wno-templates}
573 Warn when a template instantiation is encountered. Some coding
574 rules disallow templates, and this may be used to enforce that rule.
576 @item -Wunknown-pragmas
577 @cindex @option{-Wunknown-pragmas}
578 @cindex @option{-Wno-unknown-pragmas}
579 Warn when a @code{pragma()} is encountered that is not understood by
580 @command{gdc}. This differs from @option{-fignore-unknown-pragmas}
581 where a pragma that is part of the D language, but not implemented by
582 the compiler, won't get reported.
584 @item -fignore-unknown-pragmas
585 @cindex @option{-fignore-unknown-pragmas}
586 @cindex @option{-fno-ignore-unknown-pragmas}
587 Turns off errors for unsupported pragmas.
589 @item -fmax-errors=@var{n}
590 @cindex @option{-fmax-errors}
591 Limits the maximum number of error messages to @var{n}, at which point
592 @command{gdc} bails out rather than attempting to continue processing the
593 source code. If @var{n} is 0 (the default), there is no limit on the
594 number of error messages produced.
597 @cindex @option{-fsyntax-only}
598 @cindex @option{-fno-syntax-only}
599 Check the code for syntax errors, but do not actually compile it. This
600 can be used in conjunction with @option{-fdoc} or @option{-H} to generate
601 files for each module present on the command-line, but no other output
604 @item -ftransition=@var{id}
605 @cindex @option{-ftransition}
606 Report additional information about D language changes identified by
607 @var{id}. The following values are supported:
611 List information on all language changes.
613 Give deprecation messages about @option{-ftransition=import} anomalies.
615 List all usages of complex or imaginary types.
617 Implements @uref{http://wiki.dlang.org/DIP1000} (experimental).
619 Implements @uref{http://wiki.dlang.org/DIP25} (experimental).
621 List all non-mutable fields which occupy an object instance.
623 Tells the compiler to revert to using an old lookup behavior for resolving
624 unqualified symbol names, where this was done in a single pass, ignoring
625 any protection attributes. The default name lookup strategy is to use two
626 passes, the first ignoring imported declarations, and the second only
627 looking at imports. The protection (@code{private}, @code{package},
628 @code{protected}) of symbols is also enforced to resolve any conflicts
629 between private and public symbols.
631 List all hidden GC allocations.
633 List all variables going into thread local storage.
639 @section Options for Linking
640 @cindex options, linking
641 @cindex linking, static
643 These options come into play when the compiler links object files into an
644 executable output file. They are meaningless if the compiler is not doing
649 @item -defaultlib @var{libname}
650 @cindex @option{-defaultlib}
651 Specify the library to use instead of libphobos when linking. Options
652 specifying the linkage of libphobos, such as @option{-static-libphobos}
653 or @option{-shared-libphobos}, are ignored.
656 @cindex @option{-debuglib}
657 Specify the debug library to use instead of libphobos when linking.
658 This option has no effect unless the @option{-g} option was also given
659 on the command line. Options specifying the linkage of libphobos, such
660 as @option{-static-libphobos} or @option{-shared-libphobos}, are ignored.
663 @cindex @option{-nophoboslib}
664 Do not use the Phobos or D runtime library when linking. Options specifying
665 the linkage of libphobos, such as @option{-static-libphobos} or
666 @option{-shared-libphobos}, are ignored. The standard system libraries are
667 used normally, unless @option{-nostdlib} or @option{-nodefaultlibs} is used.
669 @item -shared-libphobos
670 @cindex @option{-shared-libphobos}
671 On systems that provide @file{libgphobos} and @file{libgdruntime} as a
672 shared and a static library, this option forces the use of the shared
673 version. If no shared version was built when the compiler was configured,
674 this option has no effect.
676 @item -static-libphobos
677 @cindex @option{-static-libphobos}
678 On systems that provide @file{libgphobos} and @file{libgdruntime} as a
679 shared and a static library, this option forces the use of the static
680 version. If no static version was built when the compiler was configured,
681 this option has no effect.
685 @node Developer Options
686 @section Developer Options
687 @cindex developer options
688 @cindex debug dump options
691 This section describes command-line options that are primarily of
692 interest to developers or language tooling.
696 @item -fdump-d-original
697 @cindex @option{-fdump-d-original}
698 Output the internal front-end AST after the @code{semantic3} stage.
699 This option is only useful for debugging the GNU D compiler itself.
703 Dump information about the compiler language processing stages as the source
704 program is being compiled. This includes listing all modules that are
705 processed through the @code{parse}, @code{semantic}, @code{semantic2}, and
706 @code{semantic3} stages; all @code{import} modules and their file paths;
707 and all @code{function} bodies that are being compiled.