You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: spec/betterc.dd
+74-24Lines changed: 74 additions & 24 deletions
Original file line number
Diff line number
Diff line change
@@ -4,38 +4,88 @@ $(SPEC_S Better C,
4
4
5
5
$(HEADERNAV_TOC)
6
6
7
-
`-betterC` is a command-line flag for `dmd`,
8
-
which restricts the compiler's support of certain runtime features.
9
-
Notably, D programs or libraries compiled with `betterC` aren't linked with Druntime.
10
-
The use of compile-time features is not restricted in any way.
7
+
$(P It is straightforward to link C functions and libraries into D programs.
8
+
But linking D functions and libraries into C programs is not straightforward.
9
+
)
11
10
12
-
Limiting a program or library to this subset of runtime features is useful
13
-
when targeting constrained environments where the use of such features is not practical or possible.
14
-
Additionally, this also makes embedding D libraries in larger projects easier by:
11
+
$(P D programs generally require:)
15
12
16
-
$(OL
17
-
$(LI Simplifying the process of integration at the build-system level)
18
-
$(LI 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.)
19
-
$(LI 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)
20
-
)
13
+
$(OL
14
+
$(LI The D runtime library (Phobos) to be linked in, because many features of
15
+
the core language require runtime library support.)
16
+
$(LI The `main()` function to be written in D, to ensure that the required
17
+
runtime library support is properly initialized.)
18
+
)
19
+
20
+
$(P To link D functions and libraries into C programs, it's necessary to only
21
+
require the C runtime library to be linked in. This is accomplished by defining
22
+
a subset of D that fits this requirement, called $(B BetterC).
23
+
)
24
+
25
+
$(IMPLEMENTATION_DEFINED $(B BetterC) is typically enabled by setting the $(TT -betterC)
26
+
command line flag for the implementation.
27
+
)
21
28
22
-
---
23
-
extern(C) void main()
24
-
{
25
-
import core.stdc.stdio : printf;
26
-
printf("Hello betterC\n");
27
-
}
28
-
---
29
+
$(P When $(B BetterC) is enabled, the predefined $(LINK2 version.html, version) `D_BetterC`
30
+
can be used for conditional compilation.
31
+
)
32
+
33
+
34
+
$(P An entire program can be written in $(B BetterC) by supplying a C `main()` function:)
35
+
36
+
---
37
+
extern(C) void main()
38
+
{
39
+
import core.stdc.stdio : printf;
40
+
printf("Hello betterC\n");
41
+
}
42
+
---
29
43
30
44
$(CONSOLE
31
45
> dmd -betterC hello.d && ./hello
32
46
Hello betterC
33
47
)
34
48
35
-
$(H2 $(LNAME2 consequences, Consequences))
49
+
$(P Limiting a program to this subset of runtime features is useful
50
+
when targeting constrained environments where the use of such features
51
+
is not practical or possible.
52
+
)
36
53
37
-
As no Druntime is available, many D features won't work.
38
-
For example:
54
+
$(P $(B BetterC) makes embedding D libraries in existing larger projects easier by:
55
+
)
56
+
57
+
$(OL
58
+
$(LI Simplifying the process of integration at the build-system level)
59
+
$(LI Removing the need to ensure that Druntime is properly initialized on
60
+
calls to the library, when an initialization step is not performed before
0 commit comments