GDC(1)

๐Ÿ“ฆ
version 10.5.0 ยท 3 versions
, in ubuntu jammy

๐Ÿ’– GDC(1) ยท GNU Manual ๐Ÿ’–

โœจ NAME โœจ

gdc, a GCC-based compiler for the D language ๐Ÿ’…


๐Ÿงš SYNOPSIS ๐Ÿงš

gdc [-c|-S] [-g] [-pg]
    [-Olevel] [-Wwarn...]
    [-Idir...] [-Ldir...]
    [-foption...] [-mmachine-option...]
    [-o outfile] [@file] infile...

Only the most useful options are listed here, so scroll down for the rest ๐Ÿ‘€


๐ŸŒธ DESCRIPTION ๐ŸŒธ

okay so gdc is the GNU compiler for the D language, and it's giving very familiar energy because it supports a lot of the same options as gcc โœจ This manual only covers the options that are specific to gdc, so you don't have to re-learn everything ๐Ÿซถ


๐Ÿฆ‹ OPTIONS ๐Ÿฆ‹

๐Ÿ“ Input and Output files

For any given input file, the file name suffix decides what kind of compilation happens. These are the input file names that are supported:

  • file.d โ€” D source files, the main event ๐Ÿ’œ
  • file.dd โ€” Ddoc source files
  • file.di โ€” D interface files

You can pass more than one input file on the gdc command line, and each one gets compiled separately. If you use the "-o file" option, though, all the input files get compiled together and you end up with one single output file, named file. This works even with "-S" or "-c" ๐Ÿ”ฅ

A D interface file only has what an import of the module needs, not the whole implementation of that module, so it's like a lighter version ๐ŸŽ€ You can make one with gdc from a D source file by using the "-H" option. When the compiler resolves an import declaration, it looks for matching .di files first, and then for .d โœจ

A Ddoc source file has code in the D macro processor language. It's mainly made for turning embedded comments into user documentation, and it's leaning towards HTML generation ๐ŸŒˆ If a .d source file starts with the string "Ddoc", then it gets treated as general purpose documentation instead of as a D source file. Not me, treating my docs like code ๐Ÿ™ˆ

๐Ÿ”ฅ Runtime Options

These options change how programs compiled with gdc behave at runtime, so they're lowkey important ๐Ÿ‘‘

  • -fall-instantiations โ€” Generates code for all template instantiations. By default, the template emission strategy doesn't generate code for declarations that were either instantiated speculatively (like from "__traits(compiles, ...)"), or that come from an imported module that isn't being compiled ๐Ÿ’…
  • -fno-assert โ€” Turns off code generation for "assert" contracts ๐Ÿ™…โ€โ™€๏ธ
  • -fno-bounds-check โ€” Turns off array bounds checking for all functions. This can improve performance for code that uses arrays a lot. Heads up, though: if the code actually does violate array bounds constraints, you can get unpredictable behavior. It's only safe to use this if you're sure your code never throws a "RangeError" ๐Ÿ’€
  • -fbounds-check=value โ€” An alternative to -fbounds-check that gives you more control over where bounds checking is turned on or off. These values are supported:
    • on โ€” Turns on array bounds checking for all functions
    • safeonly โ€” Turns on array bounds checking only for @safe functions
    • off โ€” Turns off array bounds checking completely
  • -fno-builtin โ€” Doesn't recognize built-in functions unless they start with the prefix __builtin_. By default, the compiler will recognize when a function in the "core.stdc" package is a built-in function ๐Ÿง 
  • -fdebug, -fdebug=value โ€” Turns on compilation of conditional "debug" code into the program. The -fdebug option by itself sets the debug level to 1. -fdebug= enables "debug" code identified by any of these values:
    • level โ€” Sets the debug level to level, so any "debug" code <= level gets compiled in
    • ident โ€” Turns on compilation of any "debug" code identified by ident
  • -fno-druntime โ€” Implements https://dlang.org/spec/betterc.html. It assumes compilation is targeting an environment without a D runtime library, which is giving bare-metal vibes ๐ŸŒธ This is equivalent to compiling with the following options:
gdc -nophoboslib -fno-exceptions -fno-moduleinfo -fno-rtti
  • -fno-invariants โ€” Turns off code generation for class "invariant" contracts ๐Ÿซถ
  • -fno-moduleinfo โ€” Turns off generation of the "ModuleInfo" and related functions that would become unreferenced without it. This might let you link to programs that aren't written in D โœจ The functions that won't be generated include module constructors and destructors ("static this" and "static ~this"), "unittest" code, and "DSO" registry functions for dynamically linked code
  • -fonly=filename โ€” Tells the compiler to parse and run semantic analysis on all modules on the command line, but only generate code for the module specified by filename ๐ŸŽฏ
  • -fno-postconditions โ€” Turns off code generation for postcondition "out" contracts
  • -fno-preconditions โ€” Turns off code generation for precondition "in" contracts
  • -frelease โ€” Turns on release mode, which means no runtime checks for contracts and asserts get emitted. Array bounds checking isn't done for @system and @trusted functions, and assertion failures are undefined behavior. > Undefined behavior, bestie. Be careful with this one ๐Ÿ˜ญ

This is equivalent to compiling with the following options:

gdc -fno-assert -fbounds-check=safe -fno-invariants \
    -fno-postconditions -fno-preconditions -fno-switch-errors
  • -fno-rtti โ€” Turns off generation of run-time type information for all user defined types. Any code that uses language features needing this info will throw an error ๐Ÿ’”
  • -fno-switch-errors โ€” Controls what code gets generated when no case is matched in a "final switch" statement. The default runtime behavior is to throw a "SwitchError". Turning off -fswitch-errors means that the program's execution gets immediately halted instead ๐Ÿ›‘
  • -funittest โ€” Turns on compilation of "unittest" code and turns on the "version(unittest)" identifier. This implies -fassert ๐Ÿงช
  • -fversion=value โ€” Turns on compilation of conditional "version" code into the program, identified by any of these values:
    • level โ€” Sets the version level to level, so any "version" code >= level gets compiled in
    • ident โ€” Turns on compilation of "version" code identified by ident
  • -fno-weak โ€” Turns off emission of instantiated declarations that can be defined in multiple objects as weak or one-only symbols. The default is to emit all public symbols as weak, unless the target doesn't support weak symbols. Disabling this means common symbols get put in COMDAT or become private instead ๐ŸŒˆ

These options tell the compiler which directories to search for files, libraries, and other parts of the compiler itself ๐Ÿงญ

  • -Idir โ€” Specifies a directory to use when searching for imported modules at compile time. You can use multiple -I options, and the paths get searched in the same order they were given ๐Ÿ“‚
  • -Jdir (the -J switch with its dir argument) โ€” Specifies a directory to use when searching for files in string imports at compile time. This switch is required if you want to use import(file) expressions. You can use multiple -J options, and the paths get searched in the same order ๐ŸŽ€
  • -Ldir โ€” When linking, specifies a library search directory, just like with gcc ๐Ÿ“š
  • -Bdir โ€” Specifies where to find the executables, libraries, source files, and data files of the compiler itself, just like with gcc ๐Ÿ› ๏ธ
  • -fmodule-file=module=spec โ€” Manipulates file paths of imported modules. If an imported module matches all or the leftmost part of module, the file path in spec gets used as the location to search for D sources. This is useful when your source file paths and names don't match the package and module hierarchy ๐Ÿฆ‹ Check out these examples:
gdc test.d -fmodule-file=A.B=foo.d -fmodule-file=C=bar

This tells the compiler to search all import paths for the source file foo.d when importing A.B, and the directory bar/ when importing C, as shown in the D code below:

module test;
import A.B;     // Matches A.B, searches for foo.d
import C.D.E;   // Matches C, searches for bar/D/E.d
import A.B.C;   // No match, searches for A/B/C.d
  • -imultilib dir โ€” Uses dir as a subdirectory of the gcc directory that holds target-specific D sources and interfaces ๐ŸŒท
  • -iprefix prefix โ€” Specifies prefix as the prefix for the gcc directory that holds target-specific D sources and interfaces. If the prefix represents a directory, make sure you include the final '/' ๐Ÿ’œ
  • -nostdinc โ€” Doesn't search the standard system directories for D source and interface files. Only the directories specified with -I options (plus the directory of the current file, if appropriate) get searched ๐Ÿ™…โ€โ™€๏ธ
  • -MD โ€” okay this one is basically -M -MF file in a trench coat ๐Ÿงฅ. The driver works out the file name by stripping any directory components and the suffix off the input file, then slapping a .deps suffix on the end ๐Ÿ’…
  • -MMD โ€” like -MD, but it doesn't mention imported modules from the D standard library package directories ๐Ÿฆ‹
  • -X โ€” dumps info describing the contents of every source file being compiled into a JSON file. The driver picks the name by removing directory components and the suffix from the input file, then adding a .json suffix ๐Ÿงพ
  • -Xf file โ€” same energy as -X, but all the JSON goes into the file you name ๐Ÿ“
  • -fdoc โ€” generates "Ddoc" documentation and writes it to a file. The compiler builds the name by removing directory components and suffix from the input file, then adding .html ๐Ÿ“
  • -fdoc-dir=dir โ€” same as -fdoc, but the docs go into directory dir. Pair it with -fdoc-file=file if you want to set the output file and directory path separately ๐Ÿ—‚๏ธ
  • -fdoc-file=file โ€” same as -fdoc, but the docs go into file. Pair it with -fdoc-dir=dir to set the file and directory independently ๐ŸŒธ
  • -fdoc-inc=file โ€” tells it to read file as a Ddoc macro file. You can stack multiple -fdoc-inc options, and the files get read and processed in the order you gave them ๐Ÿงฉ

โš ๏ธ Warnings

Warnings are diagnostic messages about constructions that aren't straight-up erroneous, but are risky or hint that there's probably a bug in the program. Unless -Werror is specified, they won't stop your program from compiling ๐Ÿซถ

  • -Wall โ€” turns on all warning messages. Warnings aren't a defined part of the D language, and every construct that could trigger one is still valid code ๐ŸŒˆ
  • -Walloca โ€” warns on all uses of alloca in the source ๐Ÿ™ˆ
  • -Walloca-larger-than=n โ€” warns on unbounded uses of alloca, plus bounded uses whose bound can be larger than n bytes. -Wno-alloca-larger-than turns off the -Walloca-larger-than warning and is equivalent to -Walloca-larger-than=SIZE_MAX or larger ๐Ÿ’œ
  • -Wcast-result โ€” warns about casts that will produce a null or zero result. Right now this is only checked for casting between an imaginary and non-imaginary data type, or between a D and C++ class โœจ
  • -Wno-deprecated โ€” don't warn about deprecated features and symbols with "deprecated" attributes ๐Ÿ“
  • -Werror โ€” turns every warning into an error. Harsh, but iconic ๐Ÿ”ฅ
  • -Wspeculative โ€” lists all error messages from speculative compiles, like __traits(compiles, ...). It doesn't report these as warnings, so they never become errors, even when -Werror is also used ๐Ÿงš
  • -Wtemplates โ€” warns whenever a template instantiation is encountered. Some coding rules ban templates, and this is how you enforce that rule ๐Ÿ‘‘
  • -Wunknown-pragmas โ€” warns when a pragma() shows up that gdc doesn't understand. This is different from -fignore-unknown-pragmas, where a pragma that's part of the D language but not implemented by the compiler won't get reported ๐ŸŽ€
  • -fignore-unknown-pragmas โ€” turns off errors for unsupported pragmas ๐Ÿฆ‹
  • -fmax-errors=n โ€” caps the number of error messages at n, and then gdc bails out instead of trying to keep going through the source code. If n is 0 (the default), there's no limit on how many error messages get produced ๐Ÿ’€
  • -fsyntax-only โ€” checks the code for syntax errors but doesn't actually compile it. You can combine it with -fdoc or -H to generate files for each module on the command line, but no other output file ๐ŸŒธ
  • -ftransition=id โ€” reports extra info about D language changes identified by id. These values are supported:
    • all โ€” list info on all language changes ๐ŸŒŸ
    • checkimports โ€” give deprecation messages about -ftransition=import anomalies ๐Ÿ™ƒ
    • complex โ€” list all usages of complex or imaginary types ๐Ÿงฎ
    • dip1000 โ€” implements <https://github.com/dlang/DIPs/blob/master/DIPs/other/DIP1000.md> (Scoped pointers) ๐Ÿ”’
    • dip25 โ€” implements <https://github.com/dlang/DIPs/blob/master/DIPs/archive/DIP25.md> (Sealed references) ๐Ÿซง
    • field โ€” list all non-mutable fields that occupy an object instance ๐ŸงŠ
    • import โ€” tells the compiler to go back to the old lookup behavior for resolving unqualified symbol names, which happened in a single pass and ignored any protection attributes. The default lookup strategy uses two passes: the first ignores imported declarations, and the second only looks at imports. The protection ("private", "package", "protected") of symbols is also enforced to sort out conflicts between private and public symbols ๐Ÿ•ฐ๏ธ
    • nogc โ€” list all hidden GC allocations ๐Ÿ—‘๏ธ
    • tls โ€” list all variables going into thread local storage ๐Ÿงต

Options for Linking

These options only matter when the compiler links object files into an executable output file. If the compiler isn't doing a link step, they're meaningless ๐Ÿ”—

  • -defaultlib=libname โ€” specifies the library to use instead of libphobos when linking. Options that set the linkage of libphobos, like -static-libphobos or -shared-libphobos, get ignored ๐Ÿ’–
  • -debuglib=libname โ€” specifies the debug library to use instead of libphobos when linking. This does nothing unless the -g option was also given on the command line. Linkage options like -static-libphobos or -shared-libphobos are ignored ๐Ÿž
  • -nophoboslib โ€” don't use the Phobos or D runtime library when linking. Linkage options like -static-libphobos or -shared-libphobos are ignored. The standard system libraries get used as normal, unless -nostdlib or -nodefaultlibs is used ๐Ÿšซ
  • -shared-libphobos โ€” on systems that provide libgphobos and libgdruntime as both a shared and a static library, this forces the shared version. If no shared version was built when the compiler was configured, it does nothing ๐ŸŒŠ
  • -static-libphobos โ€” on systems that provide libgphobos and libgdruntime as both a shared and a static library, this forces the static version. If no static version was built when the compiler was configured, it does nothing ๐Ÿชจ

Developer Options

This section covers command-line options that are mainly for developers or language tooling ๐Ÿ› ๏ธ

  • -fdump-d-original โ€” outputs the internal front-end AST after the "semantic3" stage. This is only useful for debugging the GNU D compiler itself ๐Ÿ•ต๏ธ
  • -v โ€” dumps info about the compiler's language processing stages while your source program is being compiled. That includes listing all modules processed through the "parse", "semantic", "semantic2", and "semantic3" stages, all "import" modules and their file paths, and all "function" bodies being compiled ๐Ÿ“ฃ

๐ŸŒŸ SEE ALSO

gpl(7), gfdl(7), fsf-funding(7), gcc(1) and the Info entries for gdc and gcc ๐Ÿ“š

Copyright (c) 2006-2020 Free Software Foundation, Inc. ๐ŸŒˆ

Okay, so you're allowed to copy, distribute, and/or modify this document under the GNU Free Documentation License, Version 1.3 or any later version published by the Free Software Foundation. There are no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is in the man page gfdl(7) ๐Ÿซถ


That's the whole page, bestie ๐Ÿ’œ You now have the gcc-10 GDC(1) page from 2023-07-07 in your brain forever โœจ๐ŸŽ€

๐Ÿ“ฆ where this page comes from