The other Linker Flags about Xcode

Source: Internet
Author: User
During the development of iOS, some of the static libraries (. A files) of the third party are sometimes used, and then the errors of selector not recognized are found when the compile is normal but run, causing the app to flash back. Then carefully read the documentation for the library file, and you may find a workaround in the document such as adding-OBJC or-all_load to the other Linker flags. So what are the other Linker flags for? And what role-OBJC and-all_load have played. XCODE linker flags

directory [-] Why does the linker flash the resolution linker

First, explain what the other Linker flags are for. To be blunt, the LD command has other parameters than the default parameter. The LD command implements the linker's work, detailed instructions can be viewed in the terminal man LD.

If someone doesn't know what the linker is, I can make a simple explanation.

A program from simple readable code to an executable file often goes through the following steps:

Source code > Preprocessor > Compiler > Assembler > Machine code > Linker > Executable

After a series of processing of the source file, the corresponding. obj file is generated, 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. What the linker does is link the target files with some of the libraries you use to form a complete executable file.

Perhaps I describe the more superficial, because I do not understand the very deep, I suggest you read this article, you can do the linker has a general understanding of what the linker did why it will flash back

Apple's official q&a has this passage:

The ' selector not recognized ' runtime exception occurs due to a issue between the implementation of standard UNIX static libraries, the linker and the dynamic nature of objective-c. OBJECTIVE-C does not define linker symbols to each function (or method, in objective-c)-instead, linker symbols, are onl Y generated for each class. If you extend a pre-existing class with categories, the linker does not know to associate the object code of the core Clas s implementation and the category implementation. This prevents objects created into the resulting application from responding to a selector the. Defined.

Translation, presumably means that the OBJECTIVE-C linker does not create a symbolic table for each method, but only a symbolic table for the class. In this case, if the static library defines 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, the code in the category is missing in the final executable, so the function call fails. Solving Method

Solution in the background the piece I mentioned, is in the other Linker flags with the required parameters, the parameters used in general have the following 3:

-objc

-all_load

-force_load

Here is the meaning of each parameter and the specific thing to do.

The first is-OBJC, generally this parameter is enough to solve the problem mentioned above, Apple official explained as follows:

This flag causes the linker to load every object file in the library that defines a 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 would allow the successful creation of effective objective-c static libraries that contain categories on existing Clas Ses.

To put it simply, after adding this parameter, the linker will load all the Objective-c classes and categories in the static library into the final executable file, although this may cause the executable file to become larger because of the loading of a lot of unnecessary files, but this parameter is a good solution to the problem we encountered. But is that really the case?

If the-OBJC argument is really so effective, then things will be much simpler.

Important:for 64-bit and IPhone OS applications, there is a linker bug this PREVENTS-OBJC from loading objects files fro M static libraries that contain only categories and no classes. The workaround is The-allload or-forceload flags.

When a static library has only categories and no classes, the-OBJC parameter is invalidated. At this point, 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 don't use this parameter casually. If you use more than one static library file, and then use this parameter, you are likely to encounter ld:duplicate symbol error, because the different library files may have the same target file, so it is recommended to encounter-OBJC failure to use-force_ The load parameter.

-force_load is doing the same thing as-all_load, but-force_load needs to specify the path to the library file to be loaded, so you simply load a library file without affecting the load on demand for the rest of the library files.


Original address: http://small.qiang.blog.163.com/blog/static/978493072013112571950/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.