Skip to content

Auto Link

ShenYj edited this page Feb 10, 2022 · 2 revisions

Auto-Link

简介

LC_LINKER_OPTION: 链接器的特性 Auto-Link

启用这个特性后,当我们import <模块>,不需要我们再去往链接器去配置链接参数。

比如import <framework>我们在代码里使用这个是framework格式的库文件,那么在生成目标文件时,会自动在目标文件的Mach-O中,插入(编译阶段)一个 load command,格式是LC_LINKER_OPTION,存储这样一个链接器参数-framework <framework>

探索

  • 准备代码

    #import <Foundation/Foundation.h>
    
    int main() {
    
        return 0;
    }
    
    
  • 编译 .o 脚本, 增加了一个 -fmodules 参数

    clang -x objective-c -fmodules \
    -target x86_64-apple-macos11.1 \
    -fobjc-arc \
    -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk \
    -I./Frameworks/TestExample.framework/Headers \
    -c test.m -o test.o
  • otool查看 .o

    otool -l /Users/shenyj/Documents/CodeForTest/lib/staticLib/test.o

    输出结果:

    /Users/shenyj/Documents/CodeForTest/lib/staticLib/test.o:
    Load command 0
        cmd LC_SEGMENT_64
    cmdsize 392
    segname
    vmaddr 0x0000000000000000
    vmsize 0x0000000000000078
    fileoff 856
    filesize 120
    maxprot 0x00000007
    initprot 0x00000007
    nsects 4
        flags 0x0
    Section
    sectname __text
    segname __TEXT
        addr 0x0000000000000000
        size 0x000000000000000f
        offset 856
        align 2^4 (16)
        reloff 0
        nreloc 0
        flags 0x80000400
    reserved1 0
    reserved2 0
    Section
    sectname __objc_imageinfo
    segname __DATA
        addr 0x000000000000000f
        size 0x0000000000000008
        offset 871
        align 2^0 (1)
        reloff 0
        nreloc 0
        flags 0x10000000
    reserved1 0
    reserved2 0
    Section
    sectname __compact_unwind
    segname __LD
        addr 0x0000000000000018
        size 0x0000000000000020
        offset 880
        align 2^3 (8)
        reloff 976
        nreloc 1
        flags 0x02000000
    reserved1 0
    reserved2 0
    Section
    sectname __eh_frame
    segname __TEXT
        addr 0x0000000000000038
        size 0x0000000000000040
        offset 912
        align 2^3 (8)
        reloff 0
        nreloc 0
        flags 0x6800000b
    reserved1 0
    reserved2 0
    Load command 1
        cmd LC_BUILD_VERSION
    cmdsize 24
    platform 1
        minos 11.1
        sdk 12.1
    ntools 0
    Load command 2
        cmd LC_SYMTAB
    cmdsize 24
    symoff 984
    nsyms 1
    stroff 1000
    strsize 8
    Load command 3
                cmd LC_DYSYMTAB
            cmdsize 80
        ilocalsym 0
        nlocalsym 0
        iextdefsym 0
        nextdefsym 1
        iundefsym 1
        nundefsym 0
            tocoff 0
            ntoc 0
        modtaboff 0
            nmodtab 0
    extrefsymoff 0
        nextrefsyms 0
    indirectsymoff 0
    nindirectsyms 0
        extreloff 0
            nextrel 0
        locreloff 0
            nlocrel 0
    Load command 4
        cmd LC_LINKER_OPTION
    cmdsize 40
    count 2
    string #1 -framework
    string #2 Foundation
    Load command 5
        cmd LC_LINKER_OPTION
    cmdsize 40
    count 2
    string #1 -framework
    string #2 CoreGraphics
    Load command 6
        cmd LC_LINKER_OPTION
    cmdsize 40
    count 2
    string #1 -framework
    string #2 CoreServices
    Load command 7
        cmd LC_LINKER_OPTION
    cmdsize 32
    count 2
    string #1 -framework
    string #2 IOKit
    Load command 8
        cmd LC_LINKER_OPTION
    cmdsize 40
    count 2
    string #1 -framework
    string #2 DiskArbitration
    Load command 9
        cmd LC_LINKER_OPTION
    cmdsize 40
    count 2
    string #1 -framework
    string #2 CFNetwork
    Load command 10
        cmd LC_LINKER_OPTION
    cmdsize 32
    count 2
    string #1 -framework
    string #2 Security
    Load command 11
        cmd LC_LINKER_OPTION
    cmdsize 40
    count 2
    string #1 -framework
    string #2 CoreFoundation

可以看到多出很多LC_LINKER_OPTIONload command

这也是 xcode默认执行的操作, Xcode 设置路径:

.

Getting Started

Social

Clone this wiki locally