Improve this page Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone. Page wiki View or edit the community-maintained wiki page associated with this page.

Pragmas

Pragma:
    pragma ( Identifier )
    pragma ( Identifier , ArgumentList )

Pragmas are a way to pass special information to the compiler and to add vendor specific extensions to D. Pragmas can be used by themselves terminated with a ‘;’, they can influence a statement, a block of statements, a declaration, or a block of declarations.

Pragmas can appear as either declarations, Pragma DeclarationBlock, or as statements, PragmaStatement.

pragma(ident);        // just by itself

pragma(ident) declaration; // influence one declaration

pragma(ident): // influence subsequent declarations
    declaration;
    declaration;

pragma(ident)   // influence block of declarations
{
    declaration;
    declaration;
}

pragma(ident) statement; // influence one statement

pragma(ident)   // influence block of statements
{
    statement;
    statement;
}

The kind of pragma it is determined by the Identifier. ExpressionList is a comma-separated list of AssignExpressions. The AssignExpressions must be parsable as expressions, but what they mean semantically is up to the individual pragma semantics.

Predefined Pragmas

All implementations must support these, even if by just ignoring them:

msg
Constructs a message from the arguments and prints to the standard error stream while compiling:
pragma(msg, "compiling...", 1, 1.0);
lib
Inserts a directive in the object file to link in the library specified by the AssignExpression. The AssignExpressions must be a string literal:
pragma(lib, "foo.lib");
startaddress
Puts a directive into the object file saying that the function specified in the first argument will be the start address for the program:
void foo() { ... }
pragma(startaddress, foo);
This is not normally used for application level programming, but is for specialized systems work. For applications code, the start address is taken care of by the runtime library.
mangle
Overrides the default mangling for a symbol. This allows linking to a symbol which is a D keyword, which would normally be disallowed as a symbol name:
pragma(mangle, "body")
extern(C) void body_func();

Vendor Specific Pragmas

Vendor specific pragma Identifiers can be defined if they are prefixed by the vendor's trademarked name, in a similar manner to version identifiers:

pragma(DigitalMars_funky_extension) { ... }

Compilers must diagnose an error for unrecognized Pragmas, even if they are vendor specific ones. This implies that vendor specific pragmas should be wrapped in version statements:

version (DigitalMars)
{
    pragma(DigitalMars_funky_extension)
    { ... }
}