Using GDC
Contents
Simple Compilation
Creating an executable is quite easy.
gdc main.d -o main
This will attempt to compile and link the file 'main.d' and place the output into the file 'main'. If you do not use the -o switch, then your executable will be called 'a.out'.
On a typical Unix system, you can execute the resulting program with "./main" or "./a.out". On Windows, you can run the program with "main" or "a.out".(?)
To help make a transition from DMD to GDC easier, there is the standalone program 'gdmd', distributed with GDC releases, which maps DMD's command line options to GDC. To see the available options for gdmd, type 'gdmd' or 'gdmd -help' on the command line.
Command line switches
You can always display a list of D-specific command line switches with:
gdc --help=d
Many of the options in GCC may also be applicable to GDC, such as optimization flags, -O1, -O2, -Os, -O3, or flags such as -c, which compiles a file, but does not link it, and will send the object file to "main.o", if your file is main.d
Compiler Options
Switch | Description |
---|---|
-debuglib=<lib> | Link against a debug <lib> instead of Phobos. |
-defaultlib=<lib> | Link against <lib> instead of Phobos. |
-fdeps | Print information about module dependencies. |
-fdeps=<file> | Write module dependencies to <file>. |
-fdoc | Generate Ddoc documentation. |
-fdoc-dir=<dir> | Write Ddoc documentation files to <dir>. |
-fdoc-file=<file> | Write Ddoc documentation to <file>. |
-fdoc-inc=<file> | Include a Ddoc macro <file>. |
-fintfc | Generate D interface files, |
-fintfc-dir=<dir> | Write D interface files to directory <dir>. |
-fintfc-file=<file> | Write D interface file to <file>. |
-fmake-deps | Print information about module Makefile dependencies. |
-fmake-deps=<file> | Write Makefile dependency output to <file>. |
-fmake-mdeps | Like -fmake-deps but ignore system modules. |
-fmake-mdeps=<file> | Like -fmake-deps=<file> but ignore system modules. |
-fonly=<file> | Process all modules specified on the command line, but only generate code for the module <file>. |
-fXf=<file> | Write JSON documenation to <file>. |
-imultilib <dir> | Set <dir> to be the multilib include subdirectory. |
-iprefix <path> | Specify <path> as a prefix for next two options. |
-isysroot <dir> | Set <dir> to be the system root directory. |
-isystem <dir> | Add <dir> to the start of the system include path. |
-I <dir> | Add <dir> to the list of the module import paths. |
-J <dir> | Add <dir> to the list of string import paths. |
-nophoboslib | Do not link the standard D library in the compilation. The D standard library, Phobos, and the D runtime are compiled into a single library, libgphobos2. Therefore, this option prevents linking both Phobos and the D runtime. |
-nostdinc | Do not search standard system include directories. |
-nostdlib | Do not link the standard gcc libraries in the compilation. |
Language Options
Most of these have both positive and negative forms; the negative form of -ffoo is -fno-foo. This page lists only one of these two forms, whichever one is not the default.
Switch | Description |
---|---|
-fno-assert | Don't generate runtime code for the assert keyword.
|
-fno-bounds-check | Don't generate runtime code for checking array bounds before indexing. |
-fno-builtin | Don't recognize built-in functions. It only goes as far as not recognizing user declared functions as being built-in. The compiler may still generate builtin calls internally. |
-fno-debug | Don't compile debug code.
|
-fdebug=<level> | Compile in debug code less than or equal to that in <level>. |
-fdebug=<ident> | Compile in debug code identified by <ident>. |
-fd-verbose | Print information about D language processing to stdout. |
-fd-vtls | Print information about all variables going into thread local storage to stdout. |
-fall-instantiations | Generate code for all template instantiations, not just used instantiations. |
-fno-in | Don't compile in contracts.
|
-fno-invariants | Don't compile invariant contracts.
|
-fno-emit-moduleinfo | Don't generate any ModuleInfo .
|
-fno-out | Don't compile out contracts.
|
-fproperty | Enforce @property syntax of D code. |
-frelease | Compile release version. Equivalent to -fno-invariants -fno-in -fno-out -fno-assert -fno-bounds-check. |
-funittest | Compile unittest code.
|
-fversion=<level> | Compile in version code greater than or equal to that in <level>. |
-fversion=<ident> | Compile in version code identified by <ident>. |
-Wall | Enable most warning messages. |
-Werror | Error out the compiler on warnings. |
-Wdeprecated | Enable warning of deprecated language features. |
-Wunknown-pragmas | Enable warning of unsupported pragmas. |
Extensions
Extended Assembler
GDC implements a GCC extension that allows inline assembler with D expression operands. It is available on nearly all targets, not just i386. The syntax differs from the C language extension in the following ways:
- Statements start with 'asm { ...', just like the regular DMD inline assembler.
- Instruction templates can be compile-time string constants, not just string literals. If the template is not a string literal, use parenthesis to indicate that it is not an opcode.
Unlike i386 inline assembler statements, extended assembler statements do not prevent a function from being inlined.
See the GCC manual for more information about this extension.
Example:
uint invert(uint v)
{
uint result;
version(X86)
asm{ "notl %[iov]" : [iov] "=r" (result) : "0" (v); }
else version(PPC)
asm{ "nor %[oresult],%[iv],%[iv]" : [oresult] "=r" (result) : [iv] "r" (v); }
return result;
}
Attributes
GDC supports a small subset of the GCC attributes. The syntax differs from the C language __attribute__ extension in the following ways:
- All attributes are recognised only through the 'gcc.attributes' module.
- The attribute, and all its arguments are comma-delimited CTFE strings packed in a tuple.
- Nesting (brackets) for attribute arguments are optional.
Attribute | Description |
---|---|
forceinline* | Inlines the function even if no optimization level is specified. |
flatten* | Inlines every call inside this function, if possible. |
noinline* | Prevents the function from being considered for inlining. |
target* | Specify that the function is to be compiled with different target options than specified on the command line. |
noclone* | See GCC documentation. |
section* | Place symbol in specific section. See GCC documentation. |
weak* | Mark symbol as weak. See GCC documentation. |
alias* | Mark symbol as an alias (on object file level). See GCC documentation. |
architecture specific attributes | All target specific attributes are available. See GCC documentation. |
* Being backend attributes, you can't enforce that these attributes actually take effect in user code (no static asserts!) - but you have some guarantee in that the backend will complain if it can't apply the attribute
Example:
import gcc.attributes;
@attribute("noinline") void foobar() { }
@attribute("target", ("sse3")) void sse3_func() { }
//Can be overwritten in other source files
@attribute("weak") extern(C) void c_func() {};
@attribute("alias", "c_func") void aliased_func();
//Place into "test" section
@attribute("section", "test") int value;
Known Issues
See bugzilla to see bugs that have been reported to GDC.
Some more known issues, taken from here:
- See DStress for known failing cases. (Again, may be irrelevant)
- Debugging information may have a few problems. For D symbol name demangling you need at least gdb 7.2.
- Some targets do not support once-only linking. A workaround is to manually control template emission. See the -fall-instantiations (-femit-templates in old GDC versions) option above. For Darwin, Apple's GCC 3.x compiler supports one-only linking, but GDC does not build with those sources. There are no problems with the stock GCC 4.x on Darwin.
- Complex floating point operations may not work the same as DMD.
- Some math functions behave differently due to different implementations of the extended floating-point type.
- Volatile statements may not always do the right thing.
- Because of a problem on AIX, the linker will pull in more modules than needed.
- Some C libraries (Cygwin, MinGW, AIX) don't handle floating-point formatting and parsing in a standard way.