Compiling the Protocol Buffer Compiler for Objective-C

Recently, I looked into Protocol Buffers as a format of communicating between the iPhone client and the server. I didn’t find a lot of writing on this topic when it comes to Objective-C, so I figured I will write down what I found out. So here you go.

Protocol Buffers (or Protobuf) is a structured way to encode data that was first created by Google for internal use, but has been released into the public later. It was certainly more concise than XML, but I personally still prefer something that’s more human readable like JSON. The nice thing about Protocol Buffers, though, is that many of the parsing is taken care of for you. You just need to define a proto file like this (example from Google’s official site):

message Person {
required string name = 1;
required int32 id = 2;
optional string email = 3;

enum PhoneType {
HOME = 1;
WORK = 2;

message PhoneNumber {
required string number = 1;
optional PhoneType type = 2 [default = HOME];

repeated PhoneNumber phone = 4;

And then use the protocol buffer compiler (protoc) to compile a header and implementations for the accessors (getters) and mutators (setters). You can download the Windows binary to test out what protoc does by going to their download page. However, this protocol buffer compiler will only spit out C++, Java, and Python headers, but not Objective-C. This is only for you to get an overview of what the proto file looks like and what’s generated by protoc, so you get a better idea of whether you are doing it right when you do it in Objective-C.

So what do we do when it comes to Objective-C? Luckily, there’s a open-source port by Cyrus Najmabadi that you can find here. There’s no compiled binary though, so our first step will be compiling it. Basically all you have to do is follow the steps on that page. I am listing the steps here as well, with some additional comments from me.

1. Download and unzip the latest ProtocolBuffers-*.*-Source.tar.gz file in the download section to <install_directory> on your computer.

That should be relatively straight-forward.

2. Navigate to <install_directory> and type the following commands

For those who are only familiar with GUIs, we are about to run a shell script. How you do it is with the Terminal app which should be already installed on your Mac. To navigate to the directory, you use the command cd.

3. ./

4. ./configure

At this point, I was getting a ./configure: Permission denied. I had to change the permission for execution by typing in
chmod +x configure
Depending on what your set-up is like, you might or might not set the permissions differently according to your set-up. After running the configure script, a bunch of config files, many of which under gtest\config should be generated.

5. make

Now you run the make file. A bunch of files will be generated. What you want to look for is a file called protoc that should end up in your <install_directory>/src directory.

And we are done and have a brand-spanking-new protocol buffer compiler. I shall talk about the actual usage of this compiler in the next post.

This entry was posted in Protobuf. Bookmark the permalink.

One Response to Compiling the Protocol Buffer Compiler for Objective-C

  1. Pingback: Generating Objective-C Classes from Protocol Buffer | Kelvin Kao Dev

Leave a Reply

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