Problem Compiling Flatc On Linux

969 views
Skip to first unread message

Johnny Dai

unread,
Sep 12, 2014, 3:02:22 AM9/12/14
to flatb...@googlegroups.com
Well, I download the source files of flatbuffers from github and try to compile it. 
I enter flatbuffers-master/build and use cmake .. -G "Unix Makefiles", then make, but this happens:

Building CXX object CMakeFiles/flatc.dir/src/idl_parser.cpp.o
In file included from /data/home/johnnydai/personal/flatbuffers-master/src/idl_parser.cpp:19:
/data/home/johnnydai/personal/flatbuffers-master/include/flatbuffers/flatbuffers.h:34:4: error: #error A C++11 compatible compiler is required for FlatBuffers.
/data/home/johnnydai/personal/flatbuffers-master/include/flatbuffers/flatbuffers.h:35:4: error: #error __cplusplus _MSC_VER __GNUC__ __GNUC_MINOR__ __GNUC_PATCHLEVEL__
In file included from /data/home/johnnydai/personal/flatbuffers-master/src/idl_parser.cpp:19:
/data/home/johnnydai/personal/flatbuffers-master/include/flatbuffers/flatbuffers.h: In function 'size_t flatbuffers::AlignOf()':
/data/home/johnnydai/personal/flatbuffers-master/include/flatbuffers/flatbuffers.h:143: error: expected primary-expression before ')' token
/data/home/johnnydai/personal/flatbuffers-master/include/flatbuffers/flatbuffers.h:143: error: there are no arguments to 'alignof' that depend on a template parameter, so a declaration of 'alignof' must be available
/data/home/johnnydai/personal/flatbuffers-master/include/flatbuffers/flatbuffers.h:143: note: (if you use '-fpermissive', G++ will accept your code, but allowing the use of an undeclared name is deprecated)
/data/home/johnnydai/personal/flatbuffers-master/include/flatbuffers/flatbuffers.h: At global scope:
/data/home/johnnydai/personal/flatbuffers-master/include/flatbuffers/flatbuffers.h:625: error: 'nullptr' was not declared in this scope
/data/home/johnnydai/personal/flatbuffers-master/include/flatbuffers/flatbuffers.h: In member function 'P flatbuffers::Table::GetPointer(flatbuffers::voffset_t) const':
... and so
/data/home/johnnydai/personal/flatbuffers-master/src/idl_parser.cpp:977: error: expected '}' at end of input
make[2]: *** [CMakeFiles/flatc.dir/src/idl_parser.cpp.o] Error 1
make[1]: *** [CMakeFiles/flatc.dir/all] Error 2
make: *** [all] Error 2

I'm wondering whether there is something wrong in my way of make this project.
I'm green to Flatbuffers and any help will be appreciated so much.

Wouter van Oortmerssen

unread,
Sep 12, 2014, 12:28:00 PM9/12/14
to Johnny Dai, flatb...@googlegroups.com
What version of gcc are you using? "A C++11 compatible compiler is required for FlatBuffers." is generated by FlatBuffers to detect older versions of gcc that don't have the necessary features, like alignof just below it.

--
You received this message because you are subscribed to the Google Groups "FlatBuffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flatbuffers...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Johnny Dai

unread,
Sep 12, 2014, 1:00:30 PM9/12/14
to flatb...@googlegroups.com
Thx for ur reply but my computer is not available for me at present so that I cant check what version of gcc I use, but I remember it's gcc 4,4,7 and supports C11 cuz I usually use the features of C11 such as aotu, move, and so on

Johnny Dai

unread,
Sep 12, 2014, 1:07:02 PM9/12/14
to flatb...@googlegroups.com

Wouter van Oortmerssen

unread,
Sep 12, 2014, 2:10:57 PM9/12/14
to Johnny Dai, flatb...@googlegroups.com
4.4.7 is pretty old. If you don't feel like upgrading your compiler/distro, we can probably make it work however, since the errors don't seem severe. Try:

In flatbuffers.h
1) just above the #error statement, change the constant 40603 to 40407 or whatever your version actually is.
2) but a "#define nullptr NULL" right below the include statements.
3) in the function AlignOf, change alignof to __alignof__ or __alignof (both seem to be supported by gcc, though not sure what versions).

Let me know if any errors remain after that. If this works, I can make a patch that supports your compiler officially.

Johnny Dai

unread,
Sep 12, 2014, 10:25:19 PM9/12/14
to flatb...@googlegroups.com
In fact I'd love to upgrade my compiler but I can't because it's the server of our company and so many guys use it. I once read the source of faltbuffers and guessed the errors were caused of the gcc but not sure about that. I will try in your advice and reply here once I get a conclusion.
Thank you again, it's so kind of you.

Johnny Dai

unread,
Sep 13, 2014, 4:39:54 AM9/13/14
to flatb...@googlegroups.com, stor...@gmail.com
after that, there are still some errors remaining. 
/data/home/johnnydai/personal/flatbuffers-master/src/idl_parser.cpp: In member function ‘void flatbuffers::Parser::ParseDecl()’:
/data/home/johnnydai/personal/flatbuffers-master/src/idl_parser.cpp:801: error: expected primary-expression before ‘[’ token
/data/home/johnnydai/personal/flatbuffers-master/src/idl_parser.cpp:801: error: expected primary-expression before ‘]’ token
/data/home/johnnydai/personal/flatbuffers-master/src/idl_parser.cpp:801: error: expected primary-expression before ‘const’
/data/home/johnnydai/personal/flatbuffers-master/src/idl_parser.cpp:801: error: expected primary-expression before ‘const’
/data/home/johnnydai/personal/flatbuffers-master/src/idl_parser.cpp:801: error: expected unqualified-id before ‘bool’
/data/home/johnnydai/personal/flatbuffers-master/src/idl_parser.cpp:819: error: expected primary-expression before ‘[’ token
cc1plus: warnings being treated as errors
/data/home/johnnydai/personal/flatbuffers-master/src/idl_parser.cpp:819: error: left-hand operand of comma has no effect
/data/home/johnnydai/personal/flatbuffers-master/src/idl_parser.cpp:819: error: expected primary-expression before ‘const’
/data/home/johnnydai/personal/flatbuffers-master/src/idl_parser.cpp:820: error: expected primary-expression before ‘basetype’
/data/home/johnnydai/personal/flatbuffers-master/src/idl_parser.cpp:820: error: unable to deduce ‘auto’ from ‘<expression error>’
/data/home/johnnydai/personal/flatbuffers-master/src/idl_parser.cpp:820: error: expected ‘,’ or ‘;’ before ‘{’ token
/data/home/johnnydai/personal/flatbuffers-master/src/idl_parser.cpp:819: error: unused variable ‘CheckClash’
/data/home/johnnydai/personal/flatbuffers-master/src/idl_parser.cpp:977: error: expected ‘}’ at end of input
/data/home/johnnydai/personal/flatbuffers-master/src/idl_parser.cpp: At global scope:
/data/home/johnnydai/personal/flatbuffers-master/src/idl_parser.cpp:977: error: expected ‘}’ at end of input
In file included from /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_algobase.h:66,
                 from /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/algorithm:61,
                 from /data/home/johnnydai/personal/flatbuffers-master/src/idl_parser.cpp:17:
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_pair.h: In constructor ‘std::pair<_T1, _T2>::pair(std::pair<_U1, _U2>&&) [with _U1 = flatbuffers::Value, _U2 = long int, _T1 = flatbuffers::Value, _T2 = flatbuffers::FieldDef*]’:
/data/home/johnnydai/personal/flatbuffers-master/src/idl_parser.cpp:549:   instantiated from here
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_pair.h:107: error: invalid conversion from ‘long int’ to ‘flatbuffers::FieldDef*’
make[2]: *** [CMakeFiles/flatc.dir/src/idl_parser.cpp.o] Error 1
make[1]: *** [CMakeFiles/flatc.dir/all] Error 2
make: *** [all] Error 2

For the first error, the code is:
      std::sort(fields.begin(), fields.end(), 
        [](const FieldDef *a, const FieldDef *b) -> bool {
          auto a_id = atoi(a->attributes.Lookup("id")->constant.c_str());
          auto b_id = atoi(b->attributes.Lookup("id")->constant.c_str());
          return a_id < b_id;
      });
And I modify this to:
bool cmp_fields(const FieldDef *a, const FieldDef *b) 
{                                                                                                                                                                                              
    auto a_id = atoi(a->attributes.Lookup("id")->constant.c_str());
    auto b_id = atoi(b->attributes.Lookup("id")->constant.c_str());
    return a_id < b_id;
}
    std::sort(fields.begin(), fields.end(), &cmp_fields);
and then it works.
For the second error, the code is:
      auto CheckClash = [&fields, &struct_def](const char *suffix,
                                           BaseType basetype) {
    auto len = strlen(suffix);
    for (auto it = fields.begin(); it != fields.end(); ++it) {
      auto &name = (*it)->name;
      if (name.length() > len &&
          name.compare(name.length() - len, len, suffix) == 0 &&
          (*it)->value.type.base_type != BASE_TYPE_UTYPE) {
        auto field = struct_def.fields.Lookup(
                       name.substr(0, name.length() - len));
        if (field && field->value.type.base_type == basetype)
          Error("Field " + name +
                " would clash with generated functions for field " +
                field->name);
      }
    }
  };
I think these kind of errors are caused because of my compiler, its version is too low to support the advanced grammars of cpp.

Wouter van Oortmerssen

unread,
Sep 15, 2014, 7:33:54 PM9/15/14
to Johnny Dai, flatb...@googlegroups.com
Yes, it doesn't understand C++11 lambda syntax. That will be hard to fix.

The good news is that those are only used in the source of the flatc compiler and the tests, not in the headers you include in generated code. So if you can't update your compiler, one thing that might work is to compile a flatc executable on another Linux machine, generate headers, and then those (with the fixes we discussed earlier) should compile on your server machine.

Reply all
Reply to author
Forward
0 new messages