- Home /
How to specify -ObjC in Other Linker Flag in Xcode with Unity 5
Hello,
When I try to build my Unity project to Xcode project the -ObjC in Other Linker Flag is missing but I have to add this flag.
Is it possible to add automatically this flag with Unity?
Answer by Andrey-Postelzhuk · Oct 28, 2015 at 03:18 PM
Hi. You can use third party XUPorter: https://github.com/onevcat/XUPorter
But, if you are using Unity5 it's better to use UnityEditor.iOS.Xcode.PBXProject.
Here is a documentation: http://docs.unity3d.com/ScriptReference/iOS.Xcode.PBXProject.html
Here is an example: link
This is short example:
[PostProcessBuild(700)]
public static void OnPostProcessBuild(BuildTarget target, string pathToBuiltProject)
{
if (target != BuildTarget.iOS)
{
return;
}
string projPath = pathToBuiltProject + "/Unity-iPhone.xcodeproj/project.pbxproj";
PBXProject proj = new PBXProject();
proj.ReadFromString(File.ReadAllText(projPath));
string targetGUID = proj.TargetGuidByName("Unity-iPhone");
proj.AddBuildProperty(targetGUID, "OTHER_LDFLAGS", "-ObjC");
File.WriteAllText(projPath, proj.WriteToString());
}
I just spent most of a day trying to figure out how to modify my xcode project as a post-process. This new class is great. I can add frameworks and flags so easily.
A note to anyone else: if you're not sure what a specific property string needs to be you can look in the pbx file (inside the xcodeproj) and it will have the properly formatted string there (since xcode won't display it like this).
Answer by Fattie · Jan 10, 2019 at 08:07 PM
2019...
Here's exactly how you do it with modern syntax - enjoy!
using System.IO;
using UnityEngine;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEditor.iOS.Xcode;
public class BuildPostProcessor {
[PostProcessBuildAttribute(1)]
public static void OnPostProcessBuild(BuildTarget target, string path) {
Debug.Log(">> Automation by Fattie .2019 <<");
if (target == BuildTarget.iOS) {
PBXProject project = new PBXProject();
string sPath = PBXProject.GetPBXProjectPath(path);
project.ReadFromFile(sPath);
string tn = PBXProject.GetUnityTargetName();
string g = project.TargetGuidByName(tn);
ModifyAsDesired(project, g);
File.WriteAllText(sPath, project.WriteToString());
}
}
static void ModifyAsDesired(PBXProject project, string g) {
// add hella frameworks
project.AddFrameworkToProject(g, "VideoToolbox.framework", false);
project.AddFrameworkToProject(g, "libz.tbd", false);
project.AddFrameworkToProject(g, "libbz2.tbd", false);
// go insane with build settings
project.AddBuildProperty(g,
"LIBRARY_SEARCH_PATHS",
"../FFmpeg-iOS/lib");
project.AddBuildProperty(g,
"OTHER_LDFLAGS",
"-lavcodec -lavformat -lavutil -lswresample");
}
}
Call it BuildPostProcessor.csd
and put it in /Assets/Editor.
Enjoy !
Your answer
