xcode 为 iPhone 创建静态库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/348051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 18:26:15  来源:igfitidea点击:

Creating static library for iPhone

iphonexcodestatic-libraries

提问by Martin Cote

There's an open source library that I would like to use for my iPhone app. The library is written in C and compiles with Makefiles. I would like to use a static library.

我想将一个开源库用于我的 iPhone 应用程序。该库是用 C 编写的,并使用 Makefile 进行编译。我想使用静态库。

If I add the .a file to my project dependencies, it works well with the simulator, but it doesn't link when targeting the iPhone SDK (certainly because the .a file is compiled for an Intel platform).

如果我将 .a 文件添加到我的项目依赖项中,它可以很好地与模拟器配合使用,但在以 iPhone SDK 为目标时它不会链接(当然是因为 .a 文件是为 Intel 平台编译的)。

What GCC compiler flags should I use to compile a static library for the iPhone SDK? I thought that the '-arch' option would provide me with an iPhone architecture, but no luck.

我应该使用哪些 GCC 编译器标志来为 iPhone SDK 编译静态库?我认为 '-arch' 选项将为我提供 iPhone 架构,但没有运气。

Any help would be appreciated.

任何帮助,将不胜感激。

回答by digdog

For autoconf based library, I would vote for Christopher Stawarz's build script:

对于基于 autoconf 的库,我会投票给 Christopher Stawarz 的构建脚本:

http://pseudogreen.org/blog/build_autoconfed_libs_for_iphone.html

http://pseudogreen.org/blog/build_autoconfed_libs_for_iphone.html

With this script, you can automate the whole process with single command, and easily come up with library binaries for different platforms, such as iPhoneOS 2.0/2.1/2.2/2.2.1 and iPhoneSimulator 2.0/2.1/2.2/2.2.1.

使用此脚本,您可以使用单个命令自动化整个过程,并轻松为不同平台提供库二进制文件,例如 iPhoneOS 2.0/2.1/2.2/2.2.1 和 iPhoneSimulator 2.0/2.1/2.2/2.2.1。

However, due to the change of Xcode 3.1, the $(SDKROOT) build setting had became some short names like "macosx10.5", "iphoneos2.2.1" or "iphonesimulator2.2.1". So, the way he mentioned in the article about setting search path in Xcode for library and header will not work (in Xcode 3.1). You will need to hard code the path by yourself.

然而,由于Xcode 3.1 的变化, $(SDKROOT) 构建设置变成了一些短名称,如“macosx10.5”、“iphoneos2.2.1”或“iphonesimulator2.2.1”。因此,他在关于在 Xcode 中为库和标头设置搜索路径的文章中提到的方式将不起作用(在 Xcode 3.1 中)。您需要自己对路径进行硬编码。

回答by digdog

Try compiling with arm-apple-darwin-gcc as your GCC application. You can then use lipo to merge the 2 static libraries (arm and 386) together so that the development on the sim versus the device is seamless.

尝试使用 arm-apple-darwin-gcc 作为 GCC 应用程序进行编译。然后,您可以使用 lipo 将 2 个静态库(arm 和 386)合并在一起,以便在 sim 和设备上进行无缝开发。

回答by Cadu

Just compile it normally using the x86 sdk (iphone simulator) then again using the platform for arm/ios (arm-xxx-xxx) then combine both static libs in a .a using "LIPO" (command-line tool). Works a treat and it's simpler than reinventing the wheel (at least for those smallish libs).... i did that with liblo and libdmtx and i'm not having any problems and it took me like 10 minutes to do both... :-)

只需使用 x86 sdk(iphone 模拟器)正常编译,然后再次使用 arm/ios 平台(arm-xxx-xxx),然后使用“LIPO”(命令行工具)将两个静态库合并到 .a 中。很好用,而且比重新发明轮子更简单(至少对于那些小的库来说)...我用 liblo 和 libdmtx 做到了,我没有任何问题,我花了大约 10 分钟来完成这两个... :-)

回答by Martin Cote

After several attempts to use the arm GCC compiler with the tips provided here, I gave up. In my case, it is much simpler to simply port the library to XCode than trying to compile a static library for the iPhone.

多次尝试使用 arm GCC 编译器和此处提供的提示后,我放弃了。就我而言,将库简单地移植到 XCode 比尝试为 iPhone 编译静态库要简单得多。