Skip to content

Commit b34f984

Browse files
committed
Updates to C target info
1 parent 42a41fb commit b34f984

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

docs/reference/target-language-details.mdx

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,23 @@ import {
2424
<ShowIfs>
2525
<ShowIf c>
2626

27-
In the C reactor target for Lingua Franca, reactions are written in C and the code generator generates one or more standalone C programs that can be compiled and run on several platforms. It has been tested on macOS, Linux, Windows, and at least one bare-iron embedded platform. The single-threaded version (which you get by setting the `threading` target parameter to `false`) is the most portable, requiring only a handful of common C libraries (see [Included Libraries](#included-libraries) below). The multithreaded version requires a small subset of the POSIX thread library (`pthreads`) and transparently executes in parallel on a multicore machine while preserving the deterministic semantics of Lingua Franca.
27+
The C target for Lingua Franca is used when you specify either `C` or `CCpp` in the `target` declaration in your `.lf` file:
28+
```lf
29+
target C
30+
```
31+
or
32+
```lf
33+
target CCpp
34+
```
35+
In the latter case, a C++ compiler will be used,
36+
and hence your reaction bodies can written in C++ and you can include and link to C++ code.
37+
Unlike the Cpp target, you will still access the LF API through a C API.
2838

29-
Note that C is not a safe language. There are many ways that a programmer can circumvent the semantics of Lingua Franca and introduce nondeterminism and illegal memory accesses. For example, it is easy for a programmer to mistakenly send a message that is a pointer to data on the stack. The destination reactors will very likely read invalid data. It is also easy to create memory leaks, where memory is allocated and never freed. Here, we provide some guidelines for a style for writing reactors that will be safe.
39+
In the C reactor target, reaction bodies are written in C and the code generator generates one or more standalone C programs that can be compiled and run on several platforms. It has been tested on macOS, Linux, Windows, Arduino, Zephyr, and several bare-metal embedded platforms.
3040

31-
**NOTE:** If you intend to use C++ code or import C++ libraries in the C target, we provide a special CCpp target that automatically uses a C++ compiler by default. Alternatively, you might want to use the Cpp target.
41+
The single-threaded version (which you get by setting the `threading` target parameter to `false`) is the most portable, requiring only a handful of common C libraries (see [Included Libraries](#included-libraries) below). The multithreaded version requires a small subset of the POSIX thread library (`pthreads`) and transparently executes in parallel on a multicore machine while preserving the deterministic semantics of Lingua Franca.
42+
43+
Note that C is not a safe language. There are many ways that a programmer can circumvent the semantics of Lingua Franca and introduce nondeterminism and illegal memory accesses. For example, it is easy for a programmer to mistakenly send a message that is a pointer to data on the stack. The destination reactors will very likely read invalid data. It is also easy to create memory leaks, where memory is allocated and never freed. Here, we provide some guidelines for a style for writing reactors that will be safe.
3244

3345
</ShowIf>
3446
<ShowIf cpp>
@@ -204,9 +216,7 @@ Note that for all LF statements, a final semicolon is optional. If you are writi
204216
For options to the target specification, see [detailed documentation of the target options](../introduction.md).
205217

206218
The second form, `CCpp`, is used when you wish to use a C++ compiler to compile
207-
the generated code, thereby allowing your C reactors to call C++ code.
208-
209-
\<!-- The C target uses a C compiler by default, and will fail to compile mixed C/C++ language programs. As a remedy, the `CCpp` target uses the C runtime but employs a C++ compiler to compile your program. To use it, simply replace `target C` with `target CCpp`. -->
219+
the generated code, thereby allowing your reaction bodies to be written in C++ and to call C++ code.
210220

211221
Here is a minimal example of a program written in the `CCpp` target, taken from [HelloWorldCCPP.lf](https://github.com/lf-lang/lingua-franca/blob/master/test/C/src/target/HelloWorldCCPP.lf):
212222

@@ -351,7 +361,7 @@ reactor Source(sequence: int[] = {0, 1, 2}, n_sequence: int = 3) {
351361
}
352362
```
353363

354-
This uses a [`logical` `action`](<../writing-reactors/actions.mdx#logical-actions>) to repeat the reaction, sending one element of the array in each invocation.
364+
This uses a [`logical action`](<../writing-reactors/actions.mdx#logical-actions>) to repeat the reaction, sending one element of the array in each invocation.
355365

356366
In C, arrays do not encode their own length, so a separate parameter `n_sequence` is used for the array length. Obviously, there is potential here for errors, where the array length doesn't match the length parameter.
357367

0 commit comments

Comments
 (0)