从 ios 而不是 macos 的 makefile 构建 C 库

Build C Library from makefile for ios and not macos

我有一个构建一些 C 文件的 makefile,如果我 运行 它在 M1 mac 上生成的库具有架构 arm64 我认为这是必要的他们使用 iOS 的 Xcode 项目进行编译。我发现我可以 运行 命令 otool -l libf2c.a | grep platform 应该告诉我它是为了什么而编译的,在我的例子中它 returns platform1 表示 macOS. Based on this,我想我需要 iOS 的值 platform2

这是一个问题的原因是在 Xcode 中我收到错误 ld: building for iOS, but linking in object file built for macOS, file '/Users/e.../close.o' for architecture arm64

根据我一直在研究的内容,似乎 iOS 和 macOS 具有相同的体系结构 (arm64) 但不同 'platform'?但是,我不确定平台是如何确定的。我的 makefile 中是否有一些设置需要指定平台?我假设如果我能够让平台成为 iOS 那么 Xcode 将合作并能够构建我生成的库。

通过命令行编译 iOS 的首选方法可能是使用 xcrun 命令。这将允许您为您实际想要 运行 的平台指定正确的 SDK。例如:

prompt$ xcrun --sdk iphoneos --toolchain iphoneos clang -c test.c -o test.o -arch arm64
prompt$ otool -v -l test.o | grep platform
 platform IOS

TL;DR:将您的编译器调用从普通 clang 更改为 xcrun --sdk iphoneos --toolchain iphoneos clang