|
1 | 1 | # FlatBuffers Compiler (`flatc`)
|
2 | 2 |
|
3 |
| -The main compiler for FlatBuffers is called `flatc` and is used as follows: |
| 3 | +The main compiler for FlatBuffers is called `flatc` and is used to convert |
| 4 | +schema definitions into generated code files for a variety of languages. |
| 5 | + |
| 6 | +After [building](building.md) `flatc`, it is used as follows: |
4 | 7 |
|
5 | 8 | ```sh
|
6 |
| -flatc [ GENERATOR_OPTIONS ] [ -o PATH ] [- I PATH ] FILES... [ -- BINARY_FILES... ] |
| 9 | +flatc [ GENERATOR_OPTIONS ] [ -o PATH ] [- I PATH ] |
| 10 | + FILES... |
| 11 | + [ -- BINARY_FILES... ] |
7 | 12 | ```
|
| 13 | + |
| 14 | +* The `GENERATOR_OPTIONS` specify the language(s) to compile code for as well as |
| 15 | +various features to enable/disable. |
| 16 | + |
| 17 | +* The `-o PATH` specifies the path where the generated files are placed. It |
| 18 | +defaults to the current path if not specified. |
| 19 | + |
| 20 | +* The `-I PATH` specifies the paths where included schema files are located. It |
| 21 | + defaults to the current path if not specified. |
| 22 | + |
| 23 | +## Input Files |
| 24 | + |
| 25 | +`FILES...` specifies one or more schema or data files to process. They are |
| 26 | +processed in the order provided. |
| 27 | + |
| 28 | +### Schema Files |
| 29 | + |
| 30 | +For schema files, language specifiers indicate what languages to generate code |
| 31 | +for. |
| 32 | + |
| 33 | + * `--cpp`: C++ |
| 34 | + * `--java`: Java |
| 35 | + * `--kotlin`: Kotlin |
| 36 | + * `--csharp`: C# |
| 37 | + * `--go`: Golang |
| 38 | + * `--python`: Python |
| 39 | + * `--js`: JavaScript |
| 40 | + * `--ts`: TypeScript |
| 41 | + * `--php`: PHP |
| 42 | + * `--dart`: Dart |
| 43 | + * `--lua`: Lua |
| 44 | + * `--lobster`: Lobster |
| 45 | + * `--rust`: Rust |
| 46 | + * `--swift`: Swift |
| 47 | + * `--nim`: Nim |
| 48 | + |
| 49 | +Additionally, adding: |
| 50 | + |
| 51 | + * `--grpc` Will generate RPC stub code for gRPC (not available in all |
| 52 | + languages) |
| 53 | + |
| 54 | +### Data Files |
| 55 | + |
| 56 | +If `FILES...` contain data files, they can be exported to either a binary or |
| 57 | +JSON representation. |
| 58 | + |
| 59 | +* `--binary`, `-b`: Generate a binary file containing a serialized flatbuffer. |
| 60 | +* `--json`, `-j`: Generate JSON file from a serialized flatbuffer. |
| 61 | + |
| 62 | +Both options require the corresponding schema file to be included first in the |
| 63 | +list of `FILES...`. |
| 64 | + |
| 65 | +=== "To Binary" |
| 66 | + |
| 67 | + To serialize the JSON data in `mydata.json` using the schema `myschema.fbs`: |
| 68 | + |
| 69 | + ```sh |
| 70 | + flatc --binary myschema.fbs mydata.json |
| 71 | + ``` |
| 72 | + |
| 73 | + This will generate a `mydata_wire.bin` file containing the serialized |
| 74 | + flatbuffer data. |
| 75 | + |
| 76 | +=== "To JSON" |
| 77 | + |
| 78 | + To convert the serialized binary flatbuffer `mydata.bin` using the schema |
| 79 | + `myschema.fbs` to JSON: |
| 80 | + |
| 81 | + ```sh |
| 82 | + flatc --json myschema.fbs mydata.bin |
| 83 | + ``` |
| 84 | + |
| 85 | + This will generate a `mydata.json` file. |
| 86 | + |
| 87 | + |
| 88 | + |
0 commit comments