|
| 1 | +--- |
| 2 | +title: Debugging Generated Code |
| 3 | +description: Using VS Code to debug generated code. |
| 4 | +--- |
| 5 | + |
| 6 | +## Prerequisites |
| 7 | + |
| 8 | +This page assumes you are using Visual Studio Code (VS Code). |
| 9 | +Lingua Franca files are compiled using CMake, so you should install Microsoft's |
| 10 | +[CMake Tools extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode.cmake-tools). |
| 11 | +The instructions here will work with the C, Cpp, and CCpp targets. |
| 12 | +They may work with other targets as well, but will probably require additional VS Code extensions. |
| 13 | + |
| 14 | +## Getting Started |
| 15 | + |
| 16 | +When you run `lfc` to compile a Lingua Franca program, the generated code goes into a directory |
| 17 | +called `src-gen` that will be created next to the `src` directory that contains your `.lf` files |
| 18 | +(see [Structure of an LF project](<../writing-reactors/a-first-reactor.mdx#structure-of-an-lf-project>)). |
| 19 | +You can directly edit, compile, run, and debug the generated code using VS Code. |
| 20 | +To do this, start VS Code in the directory where the generated code is. |
| 21 | +For example, if your program is `src/Foo.lf`, the following steps will get you into the generated code: |
| 22 | + |
| 23 | +``` |
| 24 | + lfc src/Foo.lf |
| 25 | + cd src-gen/Foo |
| 26 | + code . |
| 27 | +``` |
| 28 | + |
| 29 | +The structure of the generated code depends on the target you are using, but in all cases, the project includes |
| 30 | +everything needed to build and run the code. |
| 31 | +You can use VS Code's convenient Search functionality to find your reaction bodies and set breakpoints. |
| 32 | +You can also modify the code directly and recompile it using CMake, but be careful to save any changes that you |
| 33 | +want to preserve somewhere else because the next time you run `lfc`, the `src-gen` directory will be overwritten. |
| 34 | + |
| 35 | +## Running and Debugging Generated Code |
| 36 | + |
| 37 | +First note that you have to ignore the built-in VS Code run and debug menu items, which are not designed to work with CMake. |
| 38 | +That is, ignore the following menu items: |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | +If you have installed the CMake Tools extension, then you should see its icon in the left sidebar, |
| 43 | +as shown at the lower left here: |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | +In the Configure menu, select the compiler to use. |
| 48 | +Above the selection is Clang for an Apple silicon Mac. |
| 49 | +Then, instead of using the menu items to build, run, and debug, |
| 50 | +use the buttons at the _bottom_ of the screen, which look like this: |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | +The Build button will compile the code. |
| 55 | +To the right of that button are buttons for debugging and running the code. |
| 56 | + |
| 57 | +## Debugging Federated Programs |
| 58 | + |
| 59 | +For federated programs, the layout of the generated files is a bit different. |
| 60 | +If, for example, your federated program is in `src/Foo.lf` and it has a top-level reactor named `bar`, |
| 61 | +then you will find the generated code for the `bar` federate in `fed-gen/Foo/src-gen/federate__bar`. |
| 62 | +You can then run and debug that individual federate from within VS Code, but you will also have |
| 63 | +to manually run the RTI and the other federates in order for the code to start executing. |
0 commit comments