一起养成写作习惯!这是我参与「掘金日新计划 · 4 月更文挑战」的第21天,点击查看活动详情。
生成目标文件
目标文件包含了机器指令代码、数据,链接时需要的信息,符号表、调试信息,字符串表。
- 不指定
target
,默认是Mach-O 64-bit object x86_64
:
clang-xc-g-ca.c-oa.o
-x
: 指定编译文件语言类型
-g
: 生成调试信息
-c
: 生成目标文件,只运行preprocess
,compile
,assemble
,不链接
-o
: 输出文件
-I<directory>
在指定目录寻找头文件
-L <dir>
指定库文件路径(.a\.dylib
库文件)
-l<library_name>
指定链接的库文件名称(.a\.dylib
库文件)
-F<directory>
在指定目录寻找framework
头文件
-framework <framework_name>
在指定链接的framework
名称生成相应的LLVM文件格式,来进行链接时间优化,当我们配合-S使用时,生成汇编语言文件。否则生成bitcode
格式的目标文件
-flto=<value>
设置LTO的模式:full or thin
-flto
设置LTO的模式:full
-flto=ful
l, 默认值,单片(monolithic
)LTO通过将所有输入合并到单个模块中来实现此目的
-flto=thin
, 使用ThinLTO
代替
-emit-llvm
指定动态库初次安装时的默认路径,向LC_ID_DYLIB
添加安装路径,该路径作为dyld定位该库。
clang -o
是将.c源文件编译成一个可执行的二进制代码(-o选项其实是指定输出文件名,如果不加-c选项,clang默认会编译链接生成可执行文件,文件的名称由-o选项指定)。
clang -c
是使用LLVM汇编器将源文件转化为目标代码。
- 指定生成
Mach-O 64-bit x86-64
目标文件格式:
clang -x c -target x86_64-apple-macos10.15 -g -c a.c -o a.o
- 如果指定target不带apple系统版本(包括macOS,iPadOS,iOS,真机和模拟器)。例如
x86_64
,那么生成的目标文件是Linux
的ELF 64-bit
:
0. clang -x c -target x86_64 -g -c a.c -o a.o
- 编译
.m
:
clang -x objective-c -target x86_64-apple-macos10.15 -fobjc-arc -fmodules -isysroot/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk -c test.m -o test.o
clang -x c -g -target arm64-apple-ios13.5 -isysroot/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.6.sdk -c a.c -o a.o
- 编译
.mm
:
在mac上编译:
clang -x objective-c++ -target x86_64-apple-macos10.15 -std=c++11 -stdlib=libc++ -fobjc-arc -fmodules -isysroot/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk -c te st.mm -o test.o
在模拟器上编译:
clang -x objective-c -target x86_64-apple-ios13.5-simulator -fobjc-arc -fmodules -isysroot/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimlator.platform/Developer/SDKs/iPhoneSimulator13.6.sdk -c test.m -o test.o
在模拟器上链接其它三方库:
clang -x objective-c -target x86_64-apple-ios13.5-simulator -fobjc-arc -fmodules -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator13.6.sdk -I/Users/ws/Desktop/课程/Library/代码-库/AFNetworking.framework/Headers -F/Users/ws/Desktop/课程/Library/代码-库 -c test.m -o test.o
clang -target x86_64-apple-ios13.5-simulator -isysroot/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator13.6.sdk -F/Users/ws/Desktop/课程/Library/代码-库 -fobjc- arc -framework AFNetworking -v test.o -o test\
clang -target x86_64-apple-ios13.5-simulator -isysroot/Applications/Xcode.app/Contents/Developer/Platforms/i PhoneSimulator.platform/Developer/SDKs/iPhoneSimulator13.6.sdk -L/Users/ws/Desktop/课程/Library/代码-库 -fobjc- arc -lAFNetworking -dead-strip test.o -o test
编译成arm64真机:
clang -target arm64-apple-ios13.5 -isysroot/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.6.sdk -L/Users/ws/Desktop//Library/- -fobjc-arc -lAFNetworking test.o -o test\
clang -target arm64-apple-ios13.5 -isysroot/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.6.sdk -F/Users/ws/Desktop/课程/Library/代码-库 -fobjc-arc -framework AFNetworki ng test.o -o test
- 生成
dsym
文件:
clang -x c -g1 a.c -o a.o
-g1
: 将调试信息写入DWARF
格式文件