Using Google Protocol Buffers With qmake

Adapted from here:
http://www.kieltech.de/uweswiki/Google%20Protocol%20Buffers

(Original copied here for posterity)

The issue I have with this project include and extra compiler definition is that it places the generated protocol buffer classes in the build directory, which is not what I wanted.

So I modified the Protobuf.pri file to place the generated .pb.h and pb.cc output files back in the directory containing the .proto file, meaning I can use them more directly from other classes in that directory. I’m using a pretty flat directory hierarchy, though, so your mileage may vary. The nice thing also about the modified script is that you can refer to it from any subproject in a multi-project .pro file, and the relative paths should resolve automatically, with the generated files always finding their way home.

The various QMAKE_FILE_BASE, QMAKE_FILE_IN_PATH, and QMAKE_FILE_NAME variables are all undocumented, but I found some references to them here and here. Unfortunately, the official qmake reference leaves a lot to be desired, to the perpetual consternation of ambitious programmers.

Now, once you’ve added some protocol buffer .proto to your project, you’ll have to build the protoc compiler (on Linux or OS X anyway). After building the protoc protobuf compiler on Mac, you might have to copy the protoc executable to a directory in Qt Creator’s build environment PATH, which looks something like this:

/Users/someuser/Qt5.0.2/5.0.2/clang_64/bin:/usr/bin:/bin:/usr/sbin:/sbin

Unfortunately, by default, Qt Creator doesn’t include /usr/local/bin in its build environment PATH. And attempting to extend the PATH by doing $PATH:/usr/local/bin did not work for me.

So I copied my protoc to /usr/bin. You might be able to extend the PATH somehow to include /usr/local/bin, but I also wasn’t sure if this setting would carry over to other build environments (e.g. Windows or Linux), so I opted to just make protoc globally available and will probably do this in the other build environments as well.

Once you have the Protobuf.pri in place and included, and have added a PROTOS declaration to your .pro file, the build should now have a line where protoc is being run:

Note that the .variable_out settings in the qmake extra compiler definitions above mean that you don’t ever have to add the generated .pb.cc and .pb.h files to the project containing the .proto file. They are automatically added to the SOURCES and HEADERS variables when the protoc compile process runs. And they are automatically set to be cleaned when you do a project rebuild. If you do add the .pb.cc and .pb.h files to your .pro file, you’ll get duplicate make rules, and a warning that one of the make rules was ignored. Since you’re probably not going to edit the generated files anyway, it doesn’t matter much that they won’t be visible in the Qt Creator project file tree.

Don’t forget to link to the protobuf library!

One thought on “Using Google Protocol Buffers With qmake”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.