The header contains the path in the files created by `protoc`

When I call protoc like this

 protoc --cpp_out=. path/to/test.proto 

files

  • path/to/test.pb.cc and
  • path/to/test.pb.h

which I want. But since cc requires h , h included as

 #include "path/to/test.pb.h" 

what I do not want. My scons tool is protoc call protoc from the root of the project, and not from a directory that includes the source files. I did not find the obvious option in the man page or in the help text.

So, my next idea was to consider this “correct” and set up my build system, but: These two files are siblings in the directory tree, so when one includes the other, the path is not required. Even compiling manually does not work.

Can someone help me?

+4
source share
2 answers

Running find-replace on generated files is most likely easier than reorganizing your build system (use the sed command on Linux / unix).

+1
source

What I finished for my project is as follows:

  • Create the pb/ directory at the same level as your include/ and src/ directories.

  • Put the .proto files there and create a make file. Write the following in it:

     CXX = g++ CXXFLAGS = -O3 PROTOBF = $(shell find ./ -name '*.proto') SOURCES = $(subst proto,pb.cc,$(PROTOBF)) OBJECTS = $(subst proto,pb.o,$(PROTOBF)) default: $(OBJECTS) @echo -n $(SOURCES): %.pb.cc : %.proto protoc --cpp_out=. $< $(OBJECTS): %.pb.o : %.pb.cc $(CXX) $(CXXFLAGS) -c $< -o $@ 

    What will generate and build protobuffer files when called.

  • In your main makefile, simply add the following include path: -Ipb/ .

    And when including the protocol buffer header, use #include <whatever.pb.h> .

  • Add the object files generated in pb/ to your linking step. I used:

     PB_OBJS = $(shell find pb/ -name '*.pb.o') 

    And gave it to the linker along with the regular object files in obj/ .


Then you can probably call the pb/ make file from the main makefile if you want to automate it. The important point is that protoc is called from the pb/ directory or the inclusion will be ruined.

Sorry for the ugly makefiles. At least it works, and I hope this helps you ...

-1
source

All Articles