$(DDOC $(DDOC_BLANKLINE )
$(DDOC_BLANKLINE )
$(SPEC_S Better C,
$(DDOC_BLANKLINE )
$(HEADERNAV_TOC $(HEADERNAV_ITEM linking, Linking)
$(HEADERNAV_ITEM better-c, Better C)
$(HEADERNAV_SUBITEMS retained, Retained Features,
$(HEADERNAV_ITEM unittests, Running unittests in -betterC
)
)
$(HEADERNAV_ITEM consequences, Unavailable Features)
)
$(DDOC_BLANKLINE )
main()
function to be written in D, to ensure that the required
runtime library support is properly initialized.)
)
$(DDOC_BLANKLINE )
$(P 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 $(B BetterC).
)
$(DDOC_BLANKLINE )
D_BetterC
can be used for conditional compilation.
)
$(DDOC_BLANKLINE )
$(DDOC_BLANKLINE )
$(P An entire program can be written in $(B BetterC) by supplying a C main()
function:)
$(DDOC_BLANKLINE )
$(D_CODE $(D_KEYWORD extern)(C) $(D_KEYWORD void) main()
{
$(D_KEYWORD import) core.stdc.stdio : printf;
printf($(D_STRING "Hello betterC\n"));
}
)
$(DDOC_BLANKLINE )
$(CONSOLE $(GT ) dmd -betterC hello.d && ./hello
Hello betterC
)
$(DDOC_BLANKLINE )
$(P 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.
)
$(DDOC_BLANKLINE )
$(P $(B BetterC) makes embedding D libraries in existing larger projects easier by:
)
$(DDOC_BLANKLINE )
$(OL $(LI Simplifying the process of integration at the build-system level)
$(LI Removing the need to ensure that Druntime is properly initialized on
calls to the library, for situations when an initialization step is not
performed or would be difficult to insert before the library is used.)
$(LI Mixing memory management strategies (GC + manual memory management) can
be tricky, hence removing D's GC from the equation may be worthwhile sometimes.)
)
$(DDOC_BLANKLINE )
$(NOTE BetterC and $(DDLINK spec/importc, ImportC, ImportC) are 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.)
$(DDOC_BLANKLINE )
$(DDOC_BLANKLINE )
scope(exit)
)
$(LI Memory safety protections)
$(LI $(DDLINK spec/cpp_interface, Interfacing to C++, Interfacing to C++))
$(LI COM classes and C++ classes)
$(LI assert
failures are directed to the C runtime library)
$(LI switch
with strings)
$(LI final switch
)
$(LI unittest
)
$(LI $(DDSUBLINK spec/interfaceToC, calling_printf, printf
format validation))
)
$(DDOC_BLANKLINE )
-betterC
)-betterC
too.
unittest
blocks can be listed with the $(DDSUBLINK spec/traits, getUnitTests, getUnitTests
) trait:
$(DDOC_BLANKLINE )
$(D_CODE $(D_KEYWORD unittest)
{
$(D_KEYWORD assert)(0);
}
$(D_KEYWORD extern)(C) $(D_KEYWORD void) main()
{
$(D_KEYWORD static) $(D_KEYWORD foreach)(u; $(D_KEYWORD __traits)(getUnitTests, $(D_KEYWORD __traits)(parent, main)))
u();
}
)
$(DDOC_BLANKLINE )
$(CONSOLE $(GT ) dmd -betterC -unittest -run test.d
dmd_runpezoXK: foo.d:3: Assertion `0' failed.
)
$(DDOC_BLANKLINE )
However, in -betterC
, assert
expressions don't use Druntime's assert and are directed to assert
from the C runtime library instead.
$(DDOC_BLANKLINE )
synchronized
and $(MREF core, sync))
$(LI Static module constructors or destructors)
)
$(DDOC_BLANKLINE )
$(DDOC_BLANKLINE )
$(SPEC_SUBNAV_PREV_NEXT simd, Vector Extensions, importc, ImportC)
$(DDOC_BLANKLINE )
)
)