BetterC is a subset of D that doesn't depend on the D runtime library, only the C runtime library.
Warning: While the D runtime library and standard library are not linked in, it is still possible to import several packages (core.* and std.*) and use some templates, types and function declarations without bodies. However, these libraries were not written for BetterC, so you will need to determine which symbols may be used. Expect cryptic compiler or linker errors.
LinkingIt is straightforward to link C functions and libraries into D programs. But linking D functions and libraries into C programs is not straightforward.
D programs generally require:
To link D functions and libraries into C programs, it's necessary to only require the C runtime library to be linked in. This is accomplished by defining a subset of D that fits this requirement, called BetterC.
Better CImplementation Defined: BetterC is typically enabled by setting the -betterC command line flag for the implementation.
When BetterC is enabled, the predefined version D_BetterC can be used for conditional compilation.
An entire program can be written in BetterC by supplying a C main() function:
extern(C) void main() { import core.stdc.stdio : printf; printf("Hello betterC\n"); }
> dmd -betterC hello.d && ./hello Hello betterC
Limiting a program to this subset of runtime features is useful when targeting constrained environments where the use of such features is not practical or possible.
BetterC makes embedding D libraries in existing larger projects easier by:
BetterC and
ImportCare very different. ImportC is an actual C compiler. BetterC is a subset of D that relies only on the existence of the C Standard library.
Retained FeaturesNearly the full language remains available. Highlights include:
While testing can be done without the
-betterCflag, it is sometimes desirable to run the testsuite in
-betterCtoo.
unittestblocks can be listed with the
getUnitTeststrait:
unittest { assert(0); } extern(C) void main() { static foreach(u; __traits(getUnitTests, __traits(parent, main))) u(); }
> dmd -betterC -unittest -run test.d dmd_runpezoXK: foo.d:3: Assertion `0' failed.
However, in
-betterC,
assertexpressions don't use Druntime's assert and are directed to
assertfrom the C runtime library instead.
Unavailable FeaturesD features not available with BetterC:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4