zoukankan      html  css  js  c++  java
  • Xcode Build Setting-> other linker flags 设置

    Xcode Build Setting-> other linker flags 设置

    一、Xcode Help 中Other Linker Flags 定义

    在Xcode Help 中查找Build Setting 可以找到:

    Other Linker Flags (OTHER_LDFLAGS)

    Options defined in this setting are passed to invocations of the linker.

    意思是:此设置中定义的选项传递给链接器调用。(设置的选项是链接器的参数)

    链接器的简单说明:

    一个程序从简单易读的代码到可执行文件往往要经历以下步骤:

    源代码 > 预处理器 > 编译器 > 汇编器 > 机器码 > 链接器 > 可执行文件

    源文件经过一系列处理以后,会生成对应的.obj文件,然后一个项目必然会有许多.obj文件,并且这些文件之间会有各种各样的联系,例如函数调用。链接器做的事就是把这些目标文件和所用的一些库链接在一起形成一个完整的可执行文件。

    了解链接器可以参考:

    https://baike.baidu.com/item/链接器/10853221?fr=aladdin

     二、Other Linker Flags选项

    xcode中,在“Targets”选项下有Other Linker Flags选项,可以填写xcode链接器的参数,如:-ObjC-all_load-force_load、-l 等。

    xcode采用的链接器为ld–GNU,ld是GNU工具链中的一个软件,主要用于将obj文件连接成可执行文件。

    在终端中输入 man ld 可以了解更多,如下图所示:

    三、部分参数作用

    这里要说的主要参数是ld工具的参数,也是在Other Linker Flags里常用到的参数。

    上面终端下拉可以看到很多参数介绍,常用的有-ObjC,-all_load,-force_load,-l

    如图:

    1.-ObjC

     Loads all members of static archive libraries that implement an Objective-C class or category.

    设置参数-ObjC,链接器会把静态库中所有的Objective-C类和类别都加载到最后的可执行文件中。

    当使用OC写的静态类别库(Objective-C static library that contains categories),在程序编译链接时,如果不在Other Linker Flags中填写-ObjC,往往会报错,出现”selector not recognized”。

    苹果官方Q&A上有这么一段话:

    The "selector not recognized" runtime exception occurs due to an 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 for each function (or method, in Objective-C) - instead, linker symbols are only 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 class implementation and the category implementation. This prevents objects created in the resulting application from responding to a selector that is defined in the category.

    翻译过来,大概意思就是Objective-C的链接器并不会为每个方法建立符号表,而是仅仅为类建立了符号表。这样的话,如果静态库中定义了已存在的一个类的分类,链接器就会以为这个类已经存在,不会把分类和核心类的代码合起来。这样的话,在最后的可执行文件中,就会缺少分类里的代码,这样函数调用就失败了。

    还有一种情况是: 添加-ObjC参数,反而报duplicate symbol 错误, 如图所示:

    这个错误可能是打包的.a静态库有问题,之前尝试各种方法都不行,后来第三方重新打包才可以。

    2.-all_load

    强制链接器加载所有包含非ObjC的目标文档。all_load会让链接器把所有找到的目标文件都加载到可执行文件中,但是千万不要随便使用这个参数!假如你使用了不止一个静态库文件,然后又使用了这个参数,那么你很有可能会遇到ld: duplicate symbol错误,因为不同的库文件里面可能会有相同的目标文件,所以建议在遇到-ObjC失效的情况下使用-force_load参数。

    3. -force_load

    加载指定的目标文档。-force_load后面需要文档路径,如:

    -force_load所做的事情跟-all_load其实是一样的,但是-force_load需要指定要进行全部加载的库文件的路径,这样的话,你就只是完全加载了一个库文件,不影响其余库文件的按需加载。

    4. -l

     -lx         This option tells the linker to search for libx.dylib or libx.a in the library search path.  If string x is of the form y.o, then that file is searched for in the same places,but without prepending `lib' or appending `.a' or `.dylib' to the filename.

    这个选项通知链接器根据搜索路径搜索libx.dylib或libx.a。

    5. -w   

    Suppress all warning messages   禁止所有警告消息

    6. 还有一种是添加$(inherited)

    参考:

    [整理]Xcode中的$(inherited)的含义

    http://blog.csdn.net/azheng51714/article/details/48030621

     

    如果我理解错误还望大家指正;

     

    参考:

    http://blog.sina.com.cn/s/blog_4cd8dd130102v47r.html 

    http://alloc.sinaapp.com/wp/?p=272 

    http://blog.csdn.net/tammy_min/article/details/12854595

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

  • 相关阅读:
    configure: error: no acceptable cc found in $PATH
    SQL server的错误日志导致服务器C盘满
    域名/IP 正则表达式
    rpm 基本命令
    VB TO C#
    yum 的基本操作
    在服务器上怎么检查一个网站的在线连接数有多大。
    MSSQL2005不能连接远程有非法字符密码的数据库
    按账目类型和日期查看账目
    梦断代码读书笔记(二)
  • 原文地址:https://www.cnblogs.com/lulushen/p/7646635.html
Copyright © 2011-2022 走看看