Files
2025-07-15 08:46:45 -07:00

1.9 KiB

Overview

This repository serves as a complex example of how C++ tooling and cross-compilation works.

The challenge

The variables at play:

  • Protoc used to generate C++ files
  • Compilation of the generated C++ files & other library/source files.
  • Linking the correct protobuf runtime library for the target machine.

protoc from any host machine can generate portable C++ files (.cpp and .h). However, the version of protoc that is used must match the protobuf runtime library that is used.

For example, the generated header will have something like this:

#include "google/protobuf/runtime_version.h"
#if PROTOBUF_VERSION != 5029003
#error "Protobuf C++ gencode is built with an incompatible version of"
#error "Protobuf C++ headers/runtime. See"
#error "https://protobuf.dev/support/cross-version-runtime-guarantee/#cpp"
#endif

If we are trying to build an application for arm64 from a x86_64 machine, the protobuf runtime library that we use must be for that target (AND still match the protoc version that we used to generate the C++ files from .proto). This is why we use vcpkg.

See here for in-depth conversation with claude.

The solution

./build.sh # build for applications meant only for the host machine
./cross-compile-for-ios.sh # uses ios/arm64 toolchain to compile all protoc generated C++ as well as other library files. More importantly, it also ensures that it compiles (or pulls from vcpkg cache) the correct protobuf runtime version for arm64.

Please look into the scripts to see how it all comes together.

Troubleshooting

I had an issue with vcpkg whereby the protobuf was conflicting between host machine one and the target machine one. So I had to add the cmake flag -DVCPKG_BINARY_SOURCES=clear.

See this issue that I posted.