or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

cpp-core.mdflexbuffers.mdgo.mdindex.mdjava.mdjavascript.mdpython.mdrust.mdschema-compiler.mdverification.md
tile.json

schema-compiler.mddocs/

Schema Compiler (flatc)

The flatc compiler transforms .fbs schema files into language-specific generated code, binary data files, and various other output formats. It supports 15+ programming languages and provides the core tooling for the FlatBuffers ecosystem.

Capabilities

Basic Usage

The compiler takes one or more .fbs schema files and generates output according to specified options.

flatc [options] file1.fbs file2.fbs...

# Basic examples:
flatc --cpp monster.fbs           # Generate C++ headers
flatc --python --java monster.fbs # Generate both Python and Java
flatc --binary monster.fbs        # Generate binary schema files

Language Code Generation

Generate language-specific code from FlatBuffer schemas. Each language option produces idiomatic code for that platform.

# Primary language generators:
--cpp              # Generate C++ headers (.h files)
--java             # Generate Java classes (.java files)  
--python           # Generate Python modules (.py files)
--js               # Generate JavaScript (.js files)
--ts               # Generate TypeScript (.ts and .d.ts files)
--go               # Generate Go files (.go files)
--rust             # Generate Rust modules (.rs files)
--csharp           # Generate C# classes (.cs files)
--swift            # Generate Swift files (.swift files)
--kotlin           # Generate Kotlin files (.kt files) 
--dart             # Generate Dart files (.dart files)
--php              # Generate PHP classes (.php files)
--lua              # Generate Lua modules (.lua files)

# Language-specific modifiers:
--gen-mutable      # Generate mutable objects (C++, C#)
--gen-object-api   # Generate object-based API (C++, Python, etc.)
--scoped-enums     # Use scoped enums (C++)
--gen-nullable     # Generate nullable reference types (C#)
--gen-name-prefix NAME  # Add prefix to generated names

Usage Examples:

# Generate C++ with mutable objects and object API
flatc --cpp --gen-mutable --gen-object-api schema.fbs

# Generate TypeScript with specific output directory  
flatc --ts -o ./generated schema.fbs

# Generate multiple languages at once
flatc --cpp --python --java --rust schema.fbs

Schema Processing Options

Control how schemas are processed and what additional metadata is generated.

# Schema analysis and validation:
--strict-json      # Strict JSON parsing (no trailing commas, etc.)
--allow-non-utf8   # Allow non-UTF8 characters in strings
--natural-utf8     # Use natural UTF8 processing
--keep-prefix      # Keep original prefixes in generated code

# Reflection and metadata:
--reflect-types    # Generate type reflection data
--reflect-names    # Generate name reflection data (includes --reflect-types)
--bfbs-comments    # Include comments in binary schema
--bfbs-builtins    # Include builtin types in binary schema

# Include path management:
-I PATH            # Add include path for schema imports
--include-prefix PREFIX  # Prefix for include statements

Output Format Options

Generate various output formats from schemas beyond just code generation.

# Binary formats:
--binary           # Generate binary wire format (.bin files)
--schema           # Generate binary schema format (.bfbs files)

# Text formats:  
--json             # Generate JSON representation (.json files)
--jsonschema       # Generate JSON Schema (.schema files)

# Documentation:
--cpp-ptr-type T   # Set C++ pointer type (default: std::unique_ptr)
--cpp-str-type T   # Set C++ string type (default: std::string)
--cpp-str-flex-ctor  # Generate flexible string constructors

Output Control Options

Control where and how generated files are written.

# Output directory and naming:
-o PATH            # Output directory (default: current directory)
--filename-suffix SUFFIX  # Add suffix to generated filenames
--filename-ext EXT # Override file extension

# File organization:
--gen-all          # Generate code for all included schemas
--one-file         # Generate single output file (language dependent)

# Dependency handling:
--dep-file FILE    # Generate dependency file (Makefile format)
--conform PATH     # Schema conformance directory
--conform-includes # Include conformance files in check

Validation and Verification

Schema validation, conformance checking, and buffer verification options.

# Schema validation:
--warnings-as-errors    # Treat warnings as errors
--no-warnings          # Suppress warning messages
--error-on-name-clash  # Error on symbol name conflicts

# Buffer verification:
--verify               # Verify input buffer integrity
--size-prefixed        # Expect size-prefixed buffers
--raw-binary           # Input is raw binary (not size-prefixed)

# Conformance testing:
--grpc                 # Generate gRPC service code
--conform DIRECTORY    # Test schema conformance

Advanced Options

Advanced compilation features for specialized use cases.

# Performance and optimization:
--force-empty          # Force generation of empty tables
--force-defaults       # Always generate default value accessors
--no-fb-import         # Don't import flatbuffers module
--no-includes          # Don't generate include statements

# Compatibility and standards:
--cpp-field-case-style STYLE  # Field naming style (lower, upper, camel, etc.)
--java-checkerframework       # Generate Checker Framework annotations
--gen-compare                 # Generate comparison functions
--gen-object-api              # Generate native object API
--gen-onefile                 # Generate single header file (C++)

# Debugging and development:
--annotate DIRECTORY   # Annotate binary files with schema info
--print-include-paths  # Print include search paths
--version             # Print flatc version information

Schema File Format

FlatBuffer schema files (.fbs) define data structures using a simple IDL syntax.

// Basic syntax elements:

// Namespace declaration
namespace MyGame.Sample;

// Include other schemas  
include "other_schema.fbs";

// Attribute definitions
attribute "priority";

// Enum definition
enum Color : byte { Red = 0, Green, Blue = 2 }

// Struct definition (fixed-size, stored inline)
struct Vec3 {
  x: float;
  y: float; 
  z: float;
}

// Table definition (variable-size, stored with vtable)
table Monster {
  pos: Vec3;                    // Struct field
  mana: short = 150;           // Field with default value
  hp: short = 100;             // Field with default value  
  name: string;                // String field
  friendly: bool = false (deprecated); // Deprecated field
  inventory: [ubyte];          // Vector field
  color: Color = Blue;         // Enum field
  weapons: [Weapon];           // Vector of tables
  equipped: Equipment;         // Union field
  path: [Vec3];               // Vector of structs
}

// Union definition
union Equipment { Weapon, Armor }

// Another table
table Weapon {
  name: string;
  damage: short;
  (priority); // Custom attribute
}

// Root type declaration
root_type Monster;

// File identifier (4 characters)
file_identifier "MONS";

// File extension for binary files
file_extension "mon";

Generated Code Structure

The compiler generates different code structures depending on the target language, but all follow similar patterns.

C++ Generated Structure:

// Generated from monster.fbs
#ifndef FLATBUFFERS_GENERATED_MONSTER_H_
#define FLATBUFFERS_GENERATED_MONSTER_H_

#include "flatbuffers/flatbuffers.h"

namespace MyGame {
namespace Sample {

// Forward declarations
struct Vec3;
struct Monster;
struct MonsterBuilder;

// Enum
enum Color : int8_t {
  Color_Red = 0,
  Color_Green = 1, 
  Color_Blue = 2
};

// Struct
FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(4) Vec3 FLATBUFFERS_FINAL_CLASS {
private:
  float x_, y_, z_;
public:
  Vec3(float x, float y, float z) : x_(x), y_(y), z_(z) {}
  float x() const { return x_; }
  float y() const { return y_; }
  float z() const { return z_; }
};

// Table
struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
  const Vec3 *pos() const { return GetStruct<const Vec3 *>(4); }
  int16_t mana() const { return GetField<int16_t>(6, 150); }
  int16_t hp() const { return GetField<int16_t>(8, 100); }
  const flatbuffers::String *name() const { return GetPointer<const flatbuffers::String *>(10); }
  // ... more methods
};

// Builder functions
flatbuffers::Offset<Monster> CreateMonster(
  flatbuffers::FlatBufferBuilder &_fbb,
  const Vec3 *pos = nullptr,
  int16_t mana = 150,
  int16_t hp = 100,
  flatbuffers::Offset<flatbuffers::String> name = 0
  // ... more parameters
);

// Root accessor
inline const Monster *GetMonster(const void *buf) {
  return flatbuffers::GetRoot<Monster>(buf);
}

}} // namespace MyGame::Sample
#endif // FLATBUFFERS_GENERATED_MONSTER_H_

Error Handling and Diagnostics

The compiler provides detailed error messages and diagnostics for schema issues.

# Common error types and messages:

# Schema syntax errors:
# error: unknown type: 'InvalidType'
# error: expected ';' after field declaration

# Type validation errors:  
# error: field 'count' cannot have default value (vectors/tables)
# error: struct field 'pos' cannot be optional

# Name collision errors:
# error: symbol 'Monster' already defined
# error: enum value 'Red' conflicts with field name

# Include resolution errors:
# error: unable to include 'missing.fbs': file not found
# error: circular include dependency detected

Usage Example:

# Complete workflow example:

# 1. Create schema file (game.fbs)
echo 'namespace Game;
table Player { 
  name: string; 
  level: int = 1; 
  score: long; 
}
root_type Player;' > game.fbs

# 2. Generate code for multiple languages
flatc --cpp --python --js --json game.fbs

# 3. This creates:
# - game_generated.h (C++)
# - Game/Player.py (Python) 
# - game_generated.js (JavaScript)
# - game.json (JSON schema)

# 4. Generate binary data
echo '{ "name": "Alice", "level": 5, "score": 1000 }' > player.json
flatc --binary game.fbs player.json

# 5. Verify the binary
flatc --verify game.fbs player.bin