2
votes

I'm trying to use some C++ protobuffer files with my IDF project, but I'm having some issues getting it working. I've generated my test.pb.cc and test.pb.h files using protoc on my test.proto file. When I try to run "make" it fails when compiling test.pb.cc with the following error message:

In file included from /Users/ethan/Documents/Development/project/components/component/test/test/test.cpp:2:0:
/Users/ethan/Documents/Development/project/components/component/test/test/test.pb.h:10:40: fatal error: google/protobuf/port_def.inc: No such file or directory

I'm guessing this is an issue with my component.mk file? I'm not sure how to go about linking the Protobuf library in the component.mk. I got this working in a quick command line program I wrote and the makefile for that used -L ~/usr/local/lib in the LDFLAGS and -lprotobuf in the g++ command. Any ideas?

I've asked this question on esp32.com as well to try to get a wider audience, because I've really got to get this working soon. Thanks in advance for any help!

2

2 Answers

1
votes

EDIT (4/30/19): This didn't end up fixing the issue entirely, as I'm not able to fully compile my code, but it did fix the include errors, so maybe I'm somewhat on the right track. Any help is still appreciated! I'm now getting linking errors with the errors "function_from_protobuf_name is undefined...". Getting probably hundreds of lines of these errors now.

It appears that I've gotten it working now. In my project/main directory, I added the following line to my component.mk file:

CXXFLAGS += -lpthread -DGOOGLE_PROTOBUF_NO_RTTI -I/usr/local/include

I had installed Protobuf (using the default settings and instructions here) to /usr/local.

  • Protobufs requires the pthread library, hence the -lpthread flag.
  • Make failed on with the error "cannot use typeid with -fno-rtti" which was fixed by adding -DGOOGLE_PROTOBUF_NO_RTTI
  • -I/usr/local/include adds usr/local/include (where Protobuf was installed) to the list of paths searched by the compiler

My project is structured as follows:

-project
    -main
        -pb
            myfile.proto
            myfile.pb.cc
            myfile.pb.h
            myfile_c_interface.h
            myfile_c_interface.cpp
        -main.c
        -component.mk (this is the one I added the above line to)
    -Makefile
    -partitions.csv
    ...

I compiled myfile.proto like normal with protoc using the commands:

cd main/pb
protoc --cpp_out=. myfile.proto

to produce myfile.pb.cc and myfile.pb.h

For interfacing with my main.c, I wrote myfile_c_interface.h and myfile_c_interface.cpp which uses the Protobuf messages from myfile.proto as needed using their c classes, but exposes the functionality to main through c-feature-only c++ (just public functions that interact with the classes internally). Maybe not the simplest way, but it's working!

1
votes

You can't link an ESP32 application against a library that's compiled for your build architecture (unless you're compiling ON an ESP32 machine, which is highly unlikely). You will also have to compile the protobuf libraries for the ESP32. You shouldn't be using ANYTHING from /usr/local/{include,lib} in your ESP32 programs.

You will need to structure the protobuf build system into the ESP IDF system[*]. You will have protobuf library files to cross-compile AND the generated myfile.pb.cc files to cross-compile.

*: I haven't done this, so it's an exercise left for the reader.