Compiling/Building FlatBuffers schema to Java, C#, PHP, Go, C++, JavaScript, TypeScript and Python under Debian/Ubuntu

Before you start

You must have the Flatbuffers library compiled/build, you can learn how from here

 

Example Schema

Save it as schema.fbs in the folder next to flatc (FlatBuffers folder)

namespace NetObjects;

union NetParameters { NetAuth, NetMove }

enum GameRoomType : int{Wood, Metal}

enum Gender : int {Male, Female}

enum Fraction : int{Blue, Green}

enum WeaponType : int {Pistol = 0, Flamethrower, Rifle, Plasma, Laser}

enum Keys : byte { Forward, Backward, Left, Right}

table NetMessage {
  parameters:NetParameters;
}

table NetAuth{
	key:string;
}

table NetMove{
	rotation:float;
	pressedKeys:[Keys];
        time:long;  
}

root_type NetMessage;
file_identifier "MESG";

 

How to compile the schema

Get inside the Flatbuffers build folder. In case you used my article you should access the Flatbuffers directory and inside should be file called flatc (FlatBuffers folder)

 



 

C#

With C# you can choose between one big file or a folder with separate classes/enums

Run the following, folder named NetObjects will be generated in the folder next to flatc (FlatBuffers folder), inside there will be the generated .cs files

./flatc -n schema.fbs

 

Run the following, file named schema.cs will be generated in the folder next to flatc (FlatBuffers folder)

./flatc -n --gen-onefile schema.fbs

 

 

Java

Run the following, folder named NetObjects will be generated in the folder next to flatc (FlatBuffers folder), inside there will be the generated .java files

./flatc -j schema.fbs

 

C++

Run the following, file named schema_generated.h will be generated in the folder next to flatc (FlatBuffers folder)

./flatc -c schema.fbs

 

Python

Run the following, folder named NetObjects will be generated in the folder next to flatc (FlatBuffers folder), inside there will be the generated .py files

./flatc -p schema.fbs

 

Go

Run the following, folder named NetObjects will be generated in the folder next to flatc (FlatBuffers folder), inside there will be the generated .go files

./flatc -g schema.fbs

 

Javascript

Run the following, file named schema_generated.js will be generated in the folder next to flatc (FlatBuffers folder)

./flatc -s schema.fbs

PHP

Run the following, folder named NetObjects will be generated in the folder next to flatc (FlatBuffers folder), inside there will be the generated .php files

./flatc --php schema.fbs

 

TypeScript

Run the following, file named schema_generated.ts will be generated in the folder next to flatc (FlatBuffers folder)

./flatc -T schema.fbs

If you have any questions or suggestions, feel free to leave a comment.
Become a Patron!