This guide shows you how to generate the Protocol Buffers source files and library for iOS from scratch.
Generate C++ source files from the proto file
Install Protocol Buffers v3.10.1 on your local machine. Please pay attention to the section titled "Note for Mac users".
Check your installation:
protoc --version
You should have
libprotoc 3.10.1
.In the Cardboard repository, navigate to the
proto
folder.Generate the Protocol Buffers source files in C++:
protoc --proto_path=. --cpp_out=. cardboard_device.proto
You should now have cardboard_device.pb.cc and cardboard_device.pb.h.
Cross-compile the Protocol Buffers library
You will need a cross-compiled Protocol Buffers library to build the Cardboard SDK.
Clone the Protocol Buffers repository into your local machine:
git clone https://github.com/protocolbuffers/protobuf.git
Navigate into the into Protocol Buffers repository folder. Run the following commands:
git checkout v3.10.1
git submodule update --init --recursive
./autogen.sh
Create environment variables:
build_dir=`pwd`/../libprotobuf/ios darwin=darwin`uname -r` protoc=`which protoc` isysroot=`xcrun --sdk iphoneos --show-sdk-path` cflags="-Wno-unused-local-typedef -Wno-unused-function -DNDEBUG -g -O0 -pipe -fPIC -fcxx-exceptions" cxxflags="$cflags -std=c++11 -stdlib=libc++"
Create the output directories:
mkdir -p $build_dir/arch mkdir -p $build_dir/lib
Configure the 64-bit build:
./configure \ --build=x86_64-apple-$darwin \ --host=arm \ --with-protoc=$protoc \ --disable-shared \ --prefix=$build_dir \ --exec-prefix=$build_dir/arch/arm64 \ "CC=clang" \ "CFLAGS=$cflags -miphoneos-version-min=8.0 -arch arm64 -isysroot $isysroot" \ "CXX=clang" \ "CXXFLAGS=$cxxflags -miphoneos-version-min=8.0 -arch arm64 -isysroot $isysroot" \ LDFLAGS="-arch arm64 -miphoneos-version-min=8.0 -stdlib=libc++" \ "LIBS=-lc++ -lc++abi"
Build the 64-bit library:
make -j8 make install
Clean the Protocol Buffers directory:
make distclean
Configure the 32-bit library build:
./configure \ --build=x86_64-apple-$darwin \ --host=armv7-apple-$darwin \ --with-protoc=$protoc \ --disable-shared \ --prefix=$build_dir \ --exec-prefix=$build_dir/arch/armv7 \ "CC=clang" \ "CFLAGS=$cflags -miphoneos-version-min=8.0 -arch armv7 -isysroot $isysroot" \ "CXX=clang" \ "CXXFLAGS=$cxxflags -miphoneos-version-min=8.0 -arch armv7 -isysroot $isysroot" \ LDFLAGS="-arch armv7 -miphoneos-version-min=8.0 -stdlib=libc++" \ "LIBS=-lc++ -lc++abi"
Build the 32-bit library:
make -j8 make install
Build the fat library:
lipo \ $build_dir/arch/arm64/lib/libprotobuf-lite.a \ $build_dir/arch/armv7/lib/libprotobuf-lite.a \ -create \ -output $build_dir/lib/libprotobuf-lite.a
Remove unused libraries:
rm -rf $build_dir/arch
Navigate out of the
protobuf
repository. You should now have the cross-compiled library (libprotobuf-lite.a
) in thelibprotobuf/ios/lib
directory, and all theinclude
files in theinclude
(liprotobuf/ios/include
) directory.Follow the schema below to copy the
libprotobuf
directory into theproto
directory in the Cardboard repository:proto ├── cardboard_device.pb.cc ├── cardboard_device.pb.h └── libprotobuf └── ios ├── include │ └── google/* └── lib └── libprotobuf-lite.a