Flatcc
FlatBuffers Compiler and Library in C for C
Install / Use
/learn @dvidelabs/FlatccREADME
Ubuntu, macOS and Windows:
Windows:
Weekly:
The JSON parser may change the interface for parsing union vectors in a future release which requires code generation to match library versions.
FlatCC FlatBuffers in C for C
flatcc has no external dependencies except for build and compiler
tools, and the C runtime library. With concurrent Ninja builds, a small client
project can build flatcc with libraries, generate schema code, link the project
and execute a test case in a few seconds, produce binaries between 15K and 60K,
read small buffers in 30ns, build FlatBuffers in about 600ns, and with a larger
executable also handle optional json parsing or printing in less than 2 us for a
10 field mixed type message.
- Online Forums
- Introduction
- Project Details
- Reporting Bugs
- Status
- Time / Space / Usability Tradeoff
- Generated Files
- Using flatcc
- Trouble Shooting
- Quickstart
- File and Type Identifiers
- JSON Parsing and Printing
- Global Scope and Included Schema
- Required Fields and Duplicate Fields
- Fast Buffers
- Types
- Unions
- Fixed Length Arrays
- Optional Fields
- Endianness
- Pitfalls in Error Handling
- Searching and Sorting
- Null Values
- Portability Layer
- Building
- Distribution
- Running Tests on Unix
- Running Tests on Windows
- Configuration
- Using the Compiler and Builder library
- FlatBuffers Binary Format
- Security Considerations
- Style Guide
- Benchmarks
Online Forums
Introduction
This project builds flatcc, a compiler that generates FlatBuffers code for C given a FlatBuffer schema file. This introduction also creates a separate test project with the traditional monster example, here in a C version.
For now assume a Unix like system although that is not a general requirement - see also Building. You will need git, cmake, bash, a C compiler, and either the ninja build system, or make.
git clone https://github.com/dvidelabs/flatcc.git
cd flatcc
# scripts/initbuild.sh ninja
scripts/initbuild.sh make
scripts/setup.sh -a ../mymonster
ls bin
ls lib
cd ../mymonster
ls src
scripts/build.sh
ls generated
scripts/initbuild.sh is optional and chooses the build backend, which defaults
to ninja.
The setup script builds flatcc using CMake, then creates a test project directory with the monster example, and a build script which is just a small shell script. The headers and libraries are symbolically linked into the test project. You do not need CMake to build your own projects once flatcc is compiled.
To create another test project named foobar, call scripts/setup.sh -s -x ../foobar. This will avoid rebuilding the flatcc project from scratch.
Integrating FlatCC
To answer the question:
"How do I integrate FlatCC into my project with minimal dependencies"
You do not need CMake and you do not need a prebuilt runtime library.
You do need the flatcc tool to initially generate your own schema files and
then you need to update your project to:
- Include the generated header files.
- Add
include/flatcc(correct version) to the include path, e.g., using-I includeif at flatcc root. - Link with zero or more files from
src/runtime/*.c(correct version). - Possibly disable certain warnings (notably disable GCC pedantic).
- Potentially work around platform limitations, e.g., using
-DPORTABLE_HAS_INCLUDE_STDALIGN=0ifstdalign.hshould be, but isn't, available on your platform. Seeinclude/flatcc/portable/*.hfor details.
You should be able to compile a test project with just a single line like:
cc -I include src/runtime/build.c myprogram.c
If you only read flatbuffer files, and do not build, verify, parse JSON, or print JSON,
you will not need any runtime .c files.
If you have a system installed flatcc package, it might have the wrong
version of header or library files. You need to somehow override that if you
have generated files from a more recent version of flatcc.
That said, you can also use the main CMake project to build libraries, or just look at it to see which warnings and flags are important to your compiler.
See also Building.
Project Details
NOTE: see
CHANGELOG.
There are occassionally minor breaking changes as API inconsistencies
are discovered. Unless clearly stated, breaking changes will not affect
the compiled runtime library, only the header files. In case of trouble,
make sure the flatcc tool is same version as the include/flatcc
path.
The project includes:
- an executable
flatccFlatBuffers schema compiler for C and a corresponding librarylibflatcc.a. The compiler generates C header files or a binary flatbuffers schema. - a typeless runtime library
libflatccrt.afor building and verifying flatbuffers from C. Generated builder headers depend on this library. It may also be useful for other language interfaces. The library maintains a stack state to make it easy to build buffers from a parser or similar. - a small
flatcc/portableheader only library for non-C11 compliant compilers, and small helpers for all compilers including endian handling and numeric printing and parsing.
See also:
-
[Builder Interface Reference]
-
[Benchmarks]
The flatcc compiler is implemented as a standalone tool instead of
extending Googles flatc compiler in order to have a pure portable C
library implementation of the schema compiler that is designed to fail
graciously on abusive input in long running processes. It is also
believed a C version may help provide schema parsing to other language
interfaces that find interfacing with C easier than C++. The FlatBuffers
team at Googles FPL lab has been very helpful in providing feedback and
answering many questions to help ensure the best possible compatibility.
Notice the name flatcc (FlatBuffers C Compiler) vs Googles flatc.
The JSON format is compatible with Googles flatc tool. The flatc
tool converts JSON from the command line using a schema and a buffer as
input. flatcc generates schema specific code to read and write JSON
at runtime. While the flatcc approach is likely much faster and also
easier to deploy, the flatc approach is likely more convenient when
manually working with JSON such as editing game scenes. Both tools have
their place.
NOTE: Big-endian platforms are only supported as of release 0.4.0.
Reporting Bugs
If possible, please provide a short reproducible schema and source file
with a main program the returns 1 on error and 0 on success and a small
build script. Preferably generate a hexdump and call the buffer verifier
to ensure the input is valid and link with the debug library
flatccrt_d.
See also [
