Solution: UE4.27.2 Source Code Version Packaging Protobuf RepeatedField Undefined Error

Problem Description

Compiling UE 4.27.2 source code version on macOS platform failed, and the core part of the error is:

Link UE4Editor-ZProtocol.dylib: Undefined symbols for architecture x86_64: "google::protobuf::RepeatedField<int>::Reserve(int)", referenced from: FZPlayerAbilityActionParams::ToPb(idlezt::PlayerAbilityActionParams*) const in Module.ZProtocol.cpp.o .... 

Problem Analysis and Solution

The protobuf version used here is 3.9.1.

Unreal 4.27.2 source code version + Rider 2023.1 + MacbookPro 2023(M2 Max) + macOS Ventura 13.3.1(a).

Source code links:

After examining the code, I found that google::protobuf::RepeatedField exists in the file google/protobuf/repeated_field.h.

And there is a similar class named RepeatedPtrFieldBase that can be compiled normally.

// Compilation success
class PROTOBUF_EXPORT RepeatedPtrFieldBase {}

// Compilation failure
class RepeatedField final {}

Adding export to the corresponding class will make it work successfully, that is:

class PROTOBUF_EXPORT RepeatedField final

To meet the requirements of other platforms such as Android and PC, you need to modify as follows, otherwise it will not compile on these platforms.

// googe/protobuf/port_def.inc
// Add the following code:

// macOS, iOS which use clang need to export `RepeatedField` to be able to compile but other platforms don't
#ifdef __clang__
#define REPEATED_FIELD_EXPORT PROTOBUF_EXPORT
#else
#define REPEATED_FIELD_EXPORT
#endif

// google/protobuf/repeated_field.h
// Change class RepeatedField final to:
class REPEATED_FIELD_EXPORT RepeatedField final