nonocast / me

记录和分享技术的博客
http://nonocast.cn
MIT License
20 stars 0 forks source link

Swift & C (Part 0) #277

Open nonocast opened 2 years ago

nonocast commented 2 years ago

还没想好,先记录

Mach-O

foo.c

int biubiubiu(void) { return 666; }

通过clang -c foo.c就可以生成object file

➜  clang -c foo.c
➜  file foo.o
foo.o: Mach-O 64-bit object x86_64
➜  nm foo.o 
0000000000000000 T _HUI_Version
➜  lipo -info foo.o
Non-fat file: foo.o is architecture: x86_64

ok,我们可以分别单独编译出arm64和x86_64的object file,然后通过lipo打成一个fat binary

➜  clang -c -arch arm64 -o foo.o.arm64 foo.c
➜  clang -c -arch x86_64 -o foo.o.x86_64 foo.c
➜  lipo foo.o.arm64 foo.o.x86_64 -create -output foo.o
➜  libtool -static -o libfoo.a foo.o
➜    
➜  file foo.o
foo.o: Mach-O universal binary with 2 architectures: [x86_64:Mach-O 64-bit object x86_64Mach-O 64-bit object x86_64] [arm64:Mach-O 64-bit object arm64Mach-O 64-bit object arm64]
foo.o (for architecture x86_64):        Mach-O 64-bit object x86_64
foo.o (for architecture arm64): Mach-O 64-bit object arm64
➜  lipo -info libfoo.a 
Architectures in the fat file: libfoo.a are: x86_64 arm64 
➜  lipo -archs libfoo.a 
x86_64 arm64

对应的Makefile

all: libfoo.a

foo.o.arm64: foo.c
    clang -c -arch arm64 -o $@ $<

foo.o.x86_64: foo.c
    clang -c -arch x86_64 -o $@ $<

foo.o: foo.o.arm64 foo.o.x86_64
    lipo $^ -create -output $@

libfoo.a: foo.o
    libtool -static -o $@ $^

clean:
    rm *.a *.o*

.PHONY: all clean

更新May 22: BigNerd书p57: gcc -arch arm64 -arch x86_64 app.c可以直接生成fat binary.

Framework

不清楚的花,直接去看下xcrun --show-sdk-path下系统的framework,交叉比对一下。

所以,如果是需要vscode的方式,则需要手动建立目录和link,然后对整个目录进行code sign

使用时,

Test-Bridging-Header

#import <foo/foo.h>

SwiftPM

Command Tools

参考文档