6

I want to use protobuf(https://code.google.com/p/protobuf/) in my project

Did you successfully compile protobuf with xCode 5, Please help to share your experience?

Thanks.

Huy Tran
  • 217
  • 5
  • 11

2 Answers2

11

You can add support for Google Protocol Buffers to an Xcode 5 project using Cocoapods by adding the following line to your Podfile.

pod 'GoogleProtobuf', '~> 2.5.0'

This will place the C++ version of the protobuf code into a Pod for your project. It will also add the protoc compiler in the folder Pods/GoogleProtobuf/bin/protoc within your project.

You can create a custom build rule in your project that automatically converts the .proto files into .ph.{h,cc} files. Here is how I did that:

Setup a build rule to "Process Source files with names matching: *.proto Using Custom Script". The script should include the following:

cd ${INPUT_FILE_DIR}
${SRCROOT}/Pods/GoogleProtobuf/bin/protoc --proto_path=${INPUT_FILE_DIR} ${INPUT_FILE_PATH} --cpp_out=${INPUT_FILE_DIR}/cpp

Set the output files to include the following:

$(INPUT_FILE_DIR)/cpp/$(INPUT_FILE_BASE).pb.h
$(INPUT_FILE_DIR)/cpp/$(INPUT_FILE_BASE).pb.cc

Any .proto files you include in your project will now automatically be converted to C++ and then compiled as part of your build.

Bennett Smith
  • 501
  • 3
  • 12
  • Thanks! I will have a try. – Huy Tran Oct 21 '13 at 07:46
  • I have discovered an issue with the solution here. The build that is done in the Cocoapod is only usable from the iOS Simulator. I am working on a solution for this issue. For now, you could use the other method I posted in an alternative answer below. – Bennett Smith Oct 25 '13 at 06:31
  • Hello Smith, I did build protobuf(2.4.1) with CocoaPods for iPad & it works. I will have a look at your new solution. Thanks! – Huy Tran Oct 28 '13 at 07:42
  • For some reason when I'm doing this, the .proto files themselves are being copied to the output app folder. any ideas how to prevent this? – shoosh May 31 '15 at 12:17
  • @BennettSmith I tried the cocoapods solution, but I always get linker errors first beeing `Undefined symbols for architecture armv7: "_deflate", referenced from:` I tried adding `libz.dylib` as a linked binary library but that didn't help. Got a tip how to fix that? – tmbo Sep 01 '15 at 16:54
  • 1
    Did this with the homebrew flavor of Protobuf (`brew install protobuf`) instead of Cocoapods. Followed the Build Rule setup as described above and it works flawlessly! One noteworthy addendum which might not be so obvious for some (like me): "include in your project" actually means adding the `.proto` files to the `Compile Sources` Build Phase of your project. – bfx Sep 20 '19 at 14:37
10

If you don't mind building Google Protobuf yourself then a good alternative to using Cocoapods is to run the bash script here.

https://gist.github.com/BennettSmith/7150245

This script will produce a proper build of Google Protobuf that supports the i386, armv7, armv7s, arm64 and x86_64 architectures. It will produce a static library that is universal. It will also produce the protoc compiler for use on OS X.

Bennett Smith
  • 501
  • 3
  • 12
  • 1
    I just wonder this scipt will work for protobuf 2.4.1? Can I remove the build for x86_64 (just need for iOS). – Huy Tran Oct 28 '13 at 08:31
  • I am not sure about 2.4.1. Is there a specific reason to use that version instead of the newer 2.5.0? – Bennett Smith Oct 28 '13 at 18:38
  • No reason , just because my current project is using protobuf 2.4.1 – Huy Tran Oct 29 '13 at 14:58
  • I'm having trouble with the script. Scanning through the build.log file I see a few errors regarding the build. looking in the config.log file it seems there is an error when it tries to invoke clang. According to the above comments it should just work OOTB right? config.log output: http://pastebin.com/v6sEr0Ch build.log output: http://pastebin.com/gNjgU9S9 As a result nothing gets generated as far as I can tell, especially the universal or arm64 versions. :( Thanks in advance! – Michael Nguyen Mar 28 '14 at 03:45
  • @BennettSmith - Your script is great. But I'm having the same issue with arm64. We removed that section and have the script running perfectly. We're not using arm64, so it's OK for now. Just curious about it though, in case we were trying to use arm64, do you know how to fix? – Patricia May 23 '14 at 19:48
  • An updated version of the script is now available that builds Google Protobuf 2.6.0 using Xcode 6. This version includes a build for arm64, something that was not possible with the 2.5.0 version. You can find the updated script here: https://gist.github.com/BennettSmith/9487468ae3375d0db0cc – Bennett Smith Sep 21 '14 at 15:23