3

So, I am trying to compile a set of .proto files with the cmake function protobuf_generate_cpp, all of them in the same folder, and some dependent on others. The idea is to create a static library that let me use the messages in other projects.

Let's say I have the following structure in my project:

messages-lib/
 |
 +- company
 |    |
 |    +- messages
 |         |
 |         +- Header.proto // dependent on Robot.proto
 |         +- Robot.proto
 |         +- CMakeLists.txt
 +- CMakeLists.txt

Header.proto imports Robot.proto with the following line: import "company/messages/Robot.proto" because the idea is to install the library, so any other user should import common messages like that.

However, when I compile the project by using the CMake function provided by Google protobuf_generate_cpp it produces the following error: project/messages/Header.pb.cc:74:6: error: ‘::descriptor_table_company_2fmessages_2fRobot_2eproto’ has not been declared

What happend? Looking at the code generated by the CMake function, it seems that for Robot.pb.h, it defines a DescriptorTable with the follwing name: descriptor_table_Robot_2eproto but then it tries to use descriptor_table_company_2fmessages_2fRobot_2eproto in Header.pb.cc. Obviously it is not declared.

Another thing that might be important, is that I set Protobuf_IMPORT_DIRS in company/messages/CMakeLists.txt as the project directory (messages-lib), in order to be able to import the proto files as I am currently doing.

Is there a way to solve this issue? (Other than setting all the messages to LITE_RUNTIME optimization, which is not an option since I want to use reflection).

If anyone can help me would be great!

Thanks in advance!

adiego73
  • 189
  • 1
  • 3
  • 13
  • You may have `messages-lib/company/messages` in your `Protobuf_IMPORT_DIRS` for some reason. It would cause `protoc` to use that as the base directory, as it always picks the one closest to the file being compiled. – jpa Mar 29 '22 at 09:33
  • I realized I was doing an append to `Protobuf_IMPORT_DIRS`. I changed it for a set to the variable, and now it prints the root path: `/home/diego/dev/messages-lib/`. However, the problem persists.. – adiego73 Mar 29 '22 at 09:44
  • Ok, I have tried to use protoc directly: `protoc --cpp_out=build/debug --proto_path=/home/diego/dev/messages-lib /home/diego/dev/messages-lib/company/messages/Header.proto /home/diego/dev/messages-lib/company/messages/Robot.proto` and it works fine.. – adiego73 Mar 29 '22 at 13:01

0 Answers0