01/10/2025 10:53:00
Solution
Currently involved channels are Facebook, Line, Garena
MSDKGarena: In 5.17 and later versions, you need to deal with UnrealEngine's swift mixed compilation problem
MSDKFacebook: In 5.15 and later versions, you need to deal with UnrealEngine's swift mixed compilation problem
MSDKLine: In 5.7 and later versions, you need to deal with UnrealEngine's swift mixed compilation problem
Background UnrealEngine engine's usual version does not support the mixed compilation of Objective-c and Swift on the iOS platform. At present, no official solution to the problem of swift mixed compilation has been found. The following method is a solution which is based on online experience to solve the mixed compilation problem and is verified to be feasible.
Configuration operation The following uses UE4.23 as an example
1. Modify XcodeProject.cs file /Applications/UnrealEngine/UE_4.23/Engine/Source/Programs/UnrealBuildTool/ProjectFiles/Xcode/XcodeProject.cs Modified as follows: In the function:
private void AppendProjectBuildConfiguration(StringBuilder Content, string ConfigName, string ConfigGuid)
add the following code:
// Enable Swift
Content.Append("\t\t\t\tCLANG_ENABLE_MODULES = YES;" + ProjectFileGenerator.NewLine);
Content.Append("\t\t\t\tSWIFT_VERSION = 5.0;" + ProjectFileGenerator.NewLine);
Content.Append("\t\t\t\tLIBRARY_SEARCH_PATHS = \"$(TOOLCHAIN_DIR)/usr/lib/swift/$(PLATFORM_NAME)\";" + ProjectFileGenerator.NewLine);
if (ConfigName == "Debug")
{
Content.Append("\t\t\t\tSWIFT_OPTIMIZATION_LEVEL = \"-Onone\";" + ProjectFileGenerator.NewLine);
}
Content.Append("\t\t\t\tALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;" + ProjectFileGenerator.NewLine);
Content.Append("\t\t\t\tEMBEDDED_CONTENT_CONTAINS_SWIFT = YES;" + ProjectFileGenerator.NewLine);
Refer to the following:
2. Modify the IOSToolChain.cs file /Applications/UnrealEngine/UE_4.23/Engine/Source/Programs/UnrealBuildTool/Platform/IOS/IOSToolChain.cs Modified as follows: In the function:
string GetLinkArguments_Global(LinkEnvironment LinkEnvironment)
add the following code
// enable swift support
Result += " -rpath \"/usr/lib/swift\"";
Result += " -rpath \"@executable_path/Frameworks\"";
// /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/usr/lib/swift/
String swiftLibPath = String.Format(" -L {0}Platforms/{1}.platform/Developer/SDKs/{1}{2}.sdk/usr/lib/swift",
Settings.Value.XcodeDeveloperDir, bIsDevice? Settings.Value.DevicePlatformName : Settings.Value.SimulatorPlatformName, Settings.Value.IOSSDKVersion);
Result += swiftLibPath;
Log.TraceInformation("Add swift lib path : {0}", swiftLibPath);
///Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/iphoneos
swiftLibPath = String.Format(" -L {0}Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/{1}",
Settings.Value.XcodeDeveloperDir, bIsDevice? Settings.Value.DevicePlatformName.ToLower() : Settings.Value.SimulatorPlatformName.ToLower());
Result += swiftLibPath;
Log.TraceInformation("Add swift lib path : {0}", swiftLibPath);
///Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift-5.0/iphoneos
swiftLibPath = String.Format(" -L {0}Toolchains/XcodeDefault.xctoolchain/usr/lib/swift-5.0/{1}",
Settings.Value.XcodeDeveloperDir, bIsDevice? Settings.Value.DevicePlatformName.ToLower() : Settings.Value.SimulatorPlatformName.ToLower());
Result += swiftLibPath;
Example:
If you compile the code on XCode 12, you need to make the following adjustments on the above basis:
// This line of code needs to be preceded (the location of the preceding code is shown in the demo image below)
// Added by uwellpeng: enable swift support, make sure '/usr/lib/swift' goes before '@executable_path/Frameworks'
Result += " -rpath \"/usr/lib/swift\"";
// XCode 12 has more swiftcompatabiliy51 libraries, you need to add the following code
if (Settings.Value.IOSSDKVersionFloat >= 14.0f)
{
Result += String.Format(" -lswiftCompatibility51");
}
Example:
3. Recompile UBT
Use the msbuild tool to recompile UnrealBuildTool, that is, run the Terminal command msbuild in the /Applications/UnrealEngine/UE_4.23/Engine/Source/Programs/UnrealBuildTool directory
Complete the above three steps to solve the mixed compilation problem of swift on UnrealEngine
All rights reserved.