Background
During iOS development, sometimes a third-party static library (. A file) is used, and then the import finds errors that occur when the compilation is normal but runs selector not recognized
, causing the app to flash back. Then read the documentation for the library file carefully, and you may find solutions such as joining in or in the documentation Other Linker Flags
-ObjC
-all_load
.
So, what Other Linker Flags
is it used for? -ObjC
and -all_load
What role does it play?
Linker
First of all, to explain Other Linker Flags
what it is used to do. To be blunt, it is ld
a command other than the default parameter. The ld
command implements the work of the linker, which can be viewed in detail at the terminal man ld
.
If someone doesn't know what the linker is, I can make a simple statement.
A program often goes through the following steps from easy-to-read code to executable files:
Source code > Preprocessor > Compiler > Assembler > Machine code > Linker > executable file
After a series of processing, the source file will generate the corresponding .obj
file, and then a project will inevitably have many .obj
files, and there will be a variety of links between these files, such as function calls. The thing that the linker does is to link these target files with some of the libraries used to form a complete executable file.
Perhaps I describe the relatively superficial, because I know not very deep, I suggest you read this article, you can do something about the linker to have a general understanding: what the linker did
Why is it so fleeting?
Apple's official q&a has this phrase:
Objective-C
the linker does not create a symbol table for each method, but rather simply establishes a symbol table for the class. In this case, if the static library defines the classification of a class that already exists, the linker will assume that the class already exists and will not combine the classification with the code of the core class. In this case, in the final executable, the code in the taxonomy is missing, so the function call fails.
Workaround
Workaround in the background that piece I mentioned, that is, in Other Linker Flags
addition to the required parameters, the parameters used generally have the following 3:
-ObjC
-all_load
-force_load
Here is the meaning of each parameter and the specific thing to do.
First of all -ObjC
, this parameter is generally sufficient to solve the problem mentioned earlier, Apple official notes are as follows:
This flag causes the linker to the load every object file in the library, the defines an Objective-c class or category. While this option would typically result in a larger executable (due to additional object code loaded into the application) , it'll allow the successful creation of effective objective-c static libraries that contain categories on existing Clas Ses.
Simply put, after adding this parameter, the linker will load all the classes and categories in the static library Objective-C
into the final executable, although this may cause the executable to become larger because of the many unnecessary files loaded, but this parameter solves the problem we encountered. But is that really the case?
If -ObjC
the parameters are really so effective, then things will be much easier.
Important:for 64-bit and IPhone OS applications, there is a linker bugs that PREVENTS-OBJC from loading objects files fro M static libraries that contain only categories and no classes. The workaround is to use The-allload or-forceload flags.
When there are only categories in the static library and no classes, the -ObjC
parameters are invalidated. At this time, you need to use -all_load
or -force_load
.
-all_load
will let the linker load all the found target files into the executable file, but do not use this parameter arbitrarily ! If you use more than one static library file, and then use this parameter, then you are likely ld: duplicate symbol
to encounter errors, because different library files may have the same target file, so it is recommended to -ObjC
use parameters in case of failure -force_load
.
-force_load
What -all_load
you do is exactly the same, but -force_load
you need to specify the path to the library file that you want to load fully, so you simply load a library file completely without affecting the rest of the library file's on-demand loading.
About Xcode's other Linker Flags