ios 开发

链接静态库生成可执行文件

2022-12-19  本文已影响0人  iOS小洁

链接静态库.a生成可执行文件

操作文件夹:链接静态库.a文件

在生成test.o文件时,需要有TestExample.h文件

1、将test.m编译成test.o:

  1. 使用OC
  2. 生成的是X86_64_macOS架构的代码
    1. Big Sur是:x86_64-apple-macos11.1,
    2. 之前是:x86_64-apple-macos10.15
  3. 使用ARC
  4. 使用的SDK的路径在:
    1. Big Sur是:/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk
    2. 之前是:/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk
  5. 用到的其他库的头文件地址在./Frameworks
clang -x objective-c \
-target x86_64-apple-macos11.1 \
-fobjc-arc \
-isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk \
-I./StaticLibrary \
-c test.m -o test.o

将TestExample.m 编译成TestExample.o

clang -x objective-c \
-target x86_64-apple-macos11.1 \
-fobjc-arc \
-isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk \
-c TestExample.m -o TestExample.o

2、.o文件生成.a静态库

创建库命令,可以创建动态静态库

使用上面生成的TestExample.o 来创建libTestExample.a 静态库

libtool -static -arch_only x86_64 TestExample.o -o libTestExample.a

3、.o文件链接静态库,生成可执行文件

test.o链接libTestExample.a生成test可执行文件

-L./StaticLibrary 在当前目录的子目录StaticLibrary查找需要的库文件

-lTestExample 链接的名称为libTestExample/TestExample的动态库或者静态库

查找规则:先找lib+<library_name>的动态库,找不到,再去找lib+<library_name>的静态库,还找不到,就报错

-syslibroot: 系统库文件的目录

ld \
-syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -lsystem -framework Foundation \
-lTestExample \
-L./StaticLibrary \
test.o -o test

也可以使用clang

clang -target x86_64-apple-macos11.1 \
-fobjc-arc \
-isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk \
-L./StaticLibrary \
-lTestExample \
test.o -o test

链接Frameworks静态库

文件夹:静态库与Framework

1、创建.frameworks静态库

执行命令 TestExample.m --> TestExample.o --> TestExample文件

clang -x objective-c \
-target x86_64-apple-macos11.1 \
-fobjc-arc \
-isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk \
-c TestExample.m -o TestExample.o

libtool -static -arch_only x86_64 TestExample.o -o TestExample

将TestExample文件和Header文件夹放在一个文件夹内,修改名字为TestExample.framework

2、test.m 生成 test.o

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

3、test.o生成可执行文件

test.o链接TestExample.framework生成test可执行文件

-F./Frameworks 在当前目录的子目录Frameworks查找需要的库文件

-framework TestExample 链接的名称为TestExample.framework的动态库或者静态库

查找规则:先找TestExample.framework的动态库,找不到,再去找TestExample.framework的静态库,还找不到,就报错

clang -target x86_64-apple-macos11.1 \
-fobjc-arc \
-isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk \
-F./Frameworks \
-framework TestExample \
test.o -o test

静态库冲突

设置OTHER_LDFLAGS时参数:

只有设置noall_load时以下代码才能正常执行。

同时可以指定force_load,load_hidden:

//-I
HEADER_SEARCH_PATHS = $(inherited) "${SRCROOT}/AFNetworking" "${SRCROOT}/AFNetworking2"
//-L
LIBRARY_SEARCH_PATHS = $(inherited) "${SRCROOT}/AFNetworking" "${SRCROOT}/AFNetworking2"
//-l
// 冲突
// all_load
// -ObjC
// 两个静态库 -》 库
// OTHER_LDFLAGS = $(inherited) -l"AFNetworking" -l"AFNetworking2" 
// OTHER_LDFLAGS = $(inherited) -noall_load -l"AFNetworking" -l"AFNetworking2"
// OTHER_LDFLAGS = $(inherited) -all_load -l"AFNetworking" -l"AFNetworking2"
// OTHER_LDFLAGS = $(inherited) -Objc -l"AFNetworking" -l"AFNetworking2" 

OTHER_LDFLAGS = $(inherited) -l"AFNetworking" -l"AFNetworking2" -Xlinker -force_load -Xlinker "${SRCROOT}/AFNetworking/libAFNetworking.a" -Xlinker -load_hidden -Xlinker "${SRCROOT}/AFNetworking/libAFNetworking.a"
上一篇下一篇

猜你喜欢

热点阅读