Integrating flatbuffers into Visual Studio

For a recent project I wanted to automate the process of generating flatbuffers to make it as frictionaless as possible to work with flatbuffer schema files. The final process I settled on was to extend visual studio using some new configuration files. The result was a process that worked like so:

  1. Manually add a flatbuffer .fbs to the VS project (VS automatically recognizes the type)
  2. Build the project like normal, VS runs a custom pre-build event to generate the header file

I experimented with some additional features like auto-including .fbs files from a directory or automatically including the generated header files in the project but I opted to be explicit instead.

My configuration for Visual Studio 2017

I created a flatbuffers.xml that registers the filetype:

<?xml version="1.0" encoding="utf-8"?>
    <ItemType Name="Flatbuffer" DisplayName="Flattbuffer Schema" />
    <ContentType Name="FlatBuffer" DisplayName="Flatbufer Schema" ItemType="FlatBuffer" />
    <FileExtension Name="*.fbs" ContentType="Flatbuffer" />

I created a that defines the custom build event for the .fbs files:

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="">
        <PropertyPageSchema Include="$(MSBuildThisFileDirectory)flatbuffers.xml" />
        <AvailableItemName Include="Flatbuffer">

        <Message Importance="High" Text="Compiling flatbuffer files..." />

                <Message>-- Compiling %(Filename)%(Extension)</Message>
                <Command>$(SolutionDir)bin\$(Configuration)\thirdparty\flatc.exe --cpp --gen-object-api -o $(ProjectDir)%(RelativeDir) %(Fullpath)</Command>


Finally, I modified my projects .vcxproj file to import the new targets

<ImportGroup Label="ExtensionTargets">
    <Import Project="flatbuffers.targets" />

NOTE This is what the item ended up looking like in the .vcxproj file after I added it in visual studio:

    <FlatBuffer Include="myschema.fbs" />