Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
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 a local clone.

Better C

-betterC is a command-line flag for dmd, which restricts the compiler's support of certain runtime features. Notably, D programs or libraries compiled with betterC aren't linked with Druntime. The use of compile-time features is not restricted in any way.
Limiting a program or library to this subset of runtime features is useful when targeting constrained environments where the use of such features is not practical or possible. Additionally, this also makes embedding D libraries in larger projects easier by:
  1. Simplifying the process of integration at the build-system level
  2. Removing the need to ensure that Druntime is properly initialized on calls to the library, when an initialization step is not performed before the library is used.
  3. Mixing memory management strategies (GC + manual memory management) can sometimes be tricky, hence removing D's GC from the equation may be a good solution
extern(C) void main()
{
    import core.stdc.stdio : printf;
    printf("Hello betterC\n");
}
> dmd -betterC hello.d && ./hello
Hello betterC

Consequences

As no Druntime is available, many D features won't work. For example:
  1. Garbage Collection
  2. Thread-local storage
  3. TypeInfo and ModuleInfo
  4. Classes
  5. Built-in threading (e.g. core.thread)
  6. Dynamic arrays (but not slices) and associative arrays
  7. Exceptions
  8. switch with strings
  9. final switch
  10. synchronized and core.sync
  11. Static module constructors or deconstructors
  12. Struct deconstructors
  13. unittest (testing can be as usual with the -betterC flag)
The predefined version D_BetterC can be used for conditional compilation.
Vector Extensions