Take control of startup and login items Is your Mac starting up slowly? A mess of startup and login items might be to blame. Learn how to bend these computing curiosities to your will. Meshlabserver doesn't load on macOS - dyld: Library not. It will not run, i.e. When I click on the App in my Applications, it will not run. Do not understand.
-->When building your application, Visual Studio for Mac or Visual Studio callsa tool called mtouch that includes a linker for managed code. It isused to remove from the class libraries the features that theapplication is not using. The goal is to reduce the size of theapplication, which will ship with only the necessary bits.
The linker uses static analysis to determine the different code paths thatyour application is susceptible to follow. It's a bit heavy as it has to gothrough every detail of each assembly, to make sure that nothing discoverable isremoved. It is not enabled by default on the simulator builds to speedup the build time while debugging. However since it produces smaller applications it can speed up AOT compilation anduploading to the device, all devices (Release) builds are using thelinker by default.
As the linker is a static tool, it can not mark for inclusion types andmethods that are called through reflection, or dynamically instantiated. Several options exists to workaround this limitation.
Linker Behavior
The linking process can be customized via the linker behaviordropdown in Project Options. To accessthis double-click on the iOS project and browse to iOS Build > Linker Options, as illustratedbelow:
The linking process can be customized via the linker behavior dropdown in the Project Properties in Visual Studio.
Do the following:
- Right-click on the Project Name in the Solution Explorer and select Properties:
- In the Project Properties, select IOS Build:
- Follow the instructions below to change the linking options.
Mac Command Line Commands
The three main options are offered are described below:
Don't Link
Disabling linking will make sure that no assemblies are modified. Forperformance reasons this is the default setting when your IDE targetsfor the iOS simulator. For devices builds this should only be used asa workaround whenever the linker contains a bug that prevents yourapplication to run. If your application only works with -nolink,please submit a bug report.
This corresponds to the -nolink option when using the command-linetool mtouch.
Link SDK assemblies only
In this mode, the linker will leave your assemblies untouched, andwill reduce the size of the SDK assemblies (i.e. what's shipped withXamarin.iOS) by removing everything that your application doesn'tuse. This is the default setting when your IDE targets iOS devices.
This is the most simple option, as it does not require any change inyour code. The difference with linking everything is that the linkercan not perform a few optimizations in this mode, so it's a trade-offbetween the work needed to link everything and the final applicationsize.
This correspond to the -linksdk option when using the command-linetool mtouch.
Link all assemblies
When linking everything, the linker can use the whole set of itsoptimizations to make the application as small as possible. It willmodify user code, which may break whenever the code uses features in away that the linker's static analysis cannot detect. In such cases,e.g. webservices, reflection, or serialization, some adjustementsmight be required in your application to link everything.
This correspond to the -linkall option when using the command-linetool mtouch.
Controlling the Linker
When you use the linker it will sometimes will remove code that you mighthave called dynamically, even indirectly. To cover those cases the linkerprovides a few features and options to allow you greater control on itsactions.
Preserving Code
When you use the linker it can sometimes remove code that you might havecalled dynamically either using System.Reflection.MemberInfo.Invoke, or byexporting your methods to Objective-C using the
[Export]
attribute and theninvoking the selector manually.![Loaded Loaded](/uploads/1/2/6/0/126066747/426254190.png)
In those cases, you can instruct the linker to consider either entire classesto be used or individual members to be preserved by applying the
[Xamarin.iOS.Foundation.Preserve]
attribute either at the class-level or themember-level. Every member that is not statically linked by the application issubject to be removed. This attribute is hence used to mark members that are notstatically referenced, but that are still needed by your application.For instance, if you instantiate types dynamically, you may want to preservethe default constructor of your types. If you use XML serialization, you maywant to preserve the properties of your types.
You can apply this attribute on every member of a type, or on the typeitself. If you want to preserve the whole type, you can use the syntax
[Preserve (AllMembers = true)]
on the type.Sometimes you want to preserve certain members, but only if the containingtype was preserved. In those cases, use
[Preserve (Conditional=true)]
If you do not want to take a dependency on the Xamarin libraries -forexample, say that you are building a cross platform portable classlibrary (PCL) - you can still use this attribute.
To do this, you should just declare a PreserveAttribute class, and useit in your code, like this:
It does not really matter in which namespace this is defined, thelinker looks this attribute by type name.
Skipping Assemblies
It is possible to specify assemblies that should be excluded from thelinker process, while allowing other assemblies to be linked normally. This ishelpful if using
[Preserve]
on some assemblies is impossible (e.g. 3rd partycode) or as a temporary workaround for a bug.This correspond to the
--linkskip
option when using the command-linetool mtouch.When using Link All Assemblies option, if you want to tell the linker to skip entire assemblies, put the following in the Additional mtouch arguments options of your top-level assembly:
If you want the linker to skip multiple assemblies, you include multiple
linkskip
arguments:There is no user interface to use this option but it can be provided in theVisual Studio for Mac Project Options dialog or the Visual Studio project Properties pane, within the Additional mtouch arguments textfield. (E.g. --linkskip=mscorlib would not link mscorlib.dll but would linkother assemblies in the solution).
Disabling 'Link Away'
The linker will remove code that is very unlikely to be used on devices, e.g.unsupported or disallowed. In rare occasion it is possible that an applicationor library depends on this (working or not) code. Since Xamarin.iOS 5.0.1 thelinker can be instructed to skip this optimization.
This correspond to the -nolinkaway option when using thecommand-line tool mtouch.
There is no user interface to use this option but it can be provided in theVisual Studio for Mac Project Options dialog or the Visual Studio project Properties pane, within Additional mtouch arguments textfield. (E.g. --nolinkaway would not remove the extra code (about 100kb)).
Marking your Assembly as Linker-ready
Users can select to just link the SDK assemblies, and not do anylinking to their code. This also means that any third party librariesthat are not part of Xamarin's core SDK will not be linked.
This happens typically, because they do not want to manually add
[Preserve]
attributes to their code. The side effect is that thirdparty libraries will not be linked, and this in general is a gooddefault, as it is not possible to know whether a third party libraryis linker friendly or not.Mac Command Line Commands
If you have a library in your project, or you are a developer ofreusable libraries and you want the linker to treat your assembly aslinkable, all you have to do is add the assembly-level attribute
LinkerSafe
,like this:Your library does not actually need to reference the Xamarin librariesfor this. For example, if you are building a Portable Class Librarythat will run in other platforms you can still use a
LinkerSafe
attribute.The Xamarin linker looks up theLinkerSafe
attribute by name, not by its actual type. This means thatyou can write this code and it will also work:Custom Linker Configuration
Follow the instructions for creating a linker configuration file.