Search code examples
iosxcodeprotocol-buffersstatic-librariesprotobuf-c

How to link protobufs in iOS


I've been struggling trying to get protobufs 3 to compile/run for iOS. Everything compiles, but at runtime I get:

dyld: Library not loaded: /usr/local/lib/libprotobuf.15.dylib
Referenced from: /path/to/TheApp.app/TheApp
Reason: no suitable image found.  Did find: 
    /usr/local/lib/libprotobuf.15.dylib: mach-o, but not built for iOS simulator

Here are my steps:

  1. Compile static library for all architectures using this excellent script.
  2. Add the static library to the Linked Frameworks and Libraries section under the General tab for the compile target.
  3. Build => success

Solution

  • First I thought the build script wasn't actually building static libraries. I walked through the the script and checked the build output with tools like lipo and otool. Looked correct.

    Then I ran into this warning in the build output:

    URGENT: building for iOS simulator, but linking against dylib (/usr/local/lib/libprotobuf.dylib) built for OSX

    I doubled checked the build targets to ensure I was targeting iOS. Ok.

    Then I started searching for why it would link against a dynamic library even though everything was static and ran into a post on the internet that says OSX favors linking against DLLs over static libraries. If you want to fix this, you have to setup your build command some kind of style. I'm in the Xcode editor so I didn't do the exercise of translating this.

    Given this however, I deleted the reference to the dynamic library to watch the fallback to static:

    rm /usr/local/lib/libprotobuf.dylib

    Build. Run. Success.

    So the reason is known, but this answer is incomplete. Please edit this answer and provide the details for how to get Xcode to force static link vs. reaching for dylib's, vs just deleting the dylib reference