Including FlatBuffers in a CMake C++ project

2,978 views
Skip to first unread message

fairytale-recursion

unread,
Sep 16, 2018, 9:25:07 AM9/16/18
to FlatBuffers

Hey there folks,

First time posting in this community, and I am new to the space of FlatBuffers, so please bear with me!

I am building a neural network inference engine in C++, and am looking at using [Tensorflow Lite models](https://www.tensorflow.org/mobile/tflite/).  Not using Tensorflow Lite itself, just the model files.

Thus, I want to be able to parse a FlatBuffer into C++, using the structure and weights in my own way (essentially just a load of `std::vector`s for weights thus far).  However, I am having some trouble getting this working.

I managed to generate a schema using `flatc` from a pretrained `.tflite` file, but I'm not sure how to start reading the file without a build error.  I am following the [standard tutorial](https://google.github.io/flatbuffers/flatbuffers_guide_use_cpp.html).

The build error I am getting is:

/tmp/flatbuffer-mobilenet/include/schema_generated.h:589:21: error: no matching member function for call to 'Verify'
           verifier
.Verify(min()) &&


          
I have a minimum (not) working example hosted on a [Github repo](https://github.com/Wheest/flatbuffer-mobilenet).  I have provided (hopefully) clear instructions on how to get the same problem. 

I'm using CMake, which takes as an argument the include directory of `flatbuffers`.  It includes said directory with the project.

Kind regards
ftr

mikkelfj

unread,
Sep 16, 2018, 11:21:01 AM9/16/18
to FlatBuffers
I'm getting all sorts of CMake errors with your project using CMake 3.6

If I compile the schema myself I get the exact same generated file as you have in the repository.
If I compile the project manually with the following command, using clang, I get a clean compile:

c++  -std=c++17 -I include src/model_read.cpp -o model_read

But clearly I must have flatbuffers/flatbuffers.h in my system install path somewhere for this to work.

mikkelfj

unread,
Sep 16, 2018, 11:38:51 AM9/16/18
to FlatBuffers
The first CMake error I got was related to not specifing the exact command you listed in the README (cmake .. doesn't cut it).

The next error, using the suggested command was:

CMake Error in src/CMakeLists.txt:
  Target "model_read" requires the language dialect "CXX17" (with compiler
  extensions), but CMake does not know the compile flags to use to enable it.

Changing the required language standard to C++14 fixes the problem:

set(CMAKE_CXX_STANDARD 14)

You proposed build command now builds cleanly on my system.

fairytale-recursion

unread,
Sep 16, 2018, 12:07:33 PM9/16/18
to FlatBuffers
Thanks for looking at this.  The MWE doesn't require any C++17 specific features, so if you can get the CMake build working with that, that's fine.

Following you comment RE a clean clang install, I used the command

clang -std=c++17 -I include -I ${FLATBUFF_INC} src/model_read.cpp -o model_read

However, this got the same VERIFY function errors:

In file included from src/model_read.cpp:2:

include
/schema_generated.h:589:21: error: no matching member function for call to 'Verify'
           verifier
.Verify(min()) &&

           
~~~~~~~~~^~~~~~
/home/pez/tools/cpp/flatbuffers/include/flatbuffers/flatbuffers.h:1892:29: note: candidate template ignored: couldn't infer template argument 'T'
  template bool Verify(size_t elem) const {
                            ^
/home/pez/tools/cpp/flatbuffers/include/flatbuffers/flatbuffers.h:1901:29: note: candidate function template not viable: requires 2 arguments, but 1 was provided
  template bool Verify(const uint8_t *base, voffset_t elem_off)
...

The fact that you got the same generated file suggests that the problem isn't there. 

I've got flatbuffers from the latest master.  Perhaps the fact that I am only including the header files, instead of linking anything else that's been compiled might be an issue.  But as I understand it, to use flatbuffers we need a generated schema as a header file, the contents of the flatbuffers/include directory, linked to the program, which is the only thing that needs to be compiled.  Plus the data ofc.

mikkelfj

unread,
Sep 16, 2018, 12:32:43 PM9/16/18
to FlatBuffers
I suspect that you are including the wrong version of flatbuffers.h.

The command I issued:

cmake .. -DFLATBUFF_INCLUDE=$FLATBUFF_INC && make

had an unset FLATBUFF_INC shell variable, but CMake needed the define apparently and it worked because flatbuffers were in my include path.
So my build still relies on the installed flatbuffer, which I believe is v. 1.9

I don't think you need anything more than flatbuffer/{flatbuffer.h,base.h} and the generated header, so you could try fetch version 1.9 manually from the flatbuffers github repo and place it in your local include directory.

It should not be necessary to check your flatc version, becaue I already checked my output from v 1.9 to be the same as yours.

fairytale-recursion

unread,
Sep 16, 2018, 1:17:05 PM9/16/18
to FlatBuffers
Your suspicion was correct, thank you.

I checked out version v1.9.0 using
git checkout tags/v1.9.0 -b work

Building the program now works!  I'll try and remember in future that version tags are a good heuristic

RE the unset variable, in the README, I set the path to the flatbuffers repo using that variable, and CMake wants it passed.  I'm still trying to figure out a good way to remove this dependency, and I guess pull from the flatbuffers repo using CMake.  Not something I've done before, but I'm sure there's a standard way of doing it.

For now, my task is to see how to get information from the flatbuffer, but since the program now builds, it should be tutorial track stuff. 

Once again, thanks for your help
Reply all
Reply to author
Forward
0 new messages