简体   繁体   English

为iOS编译C源代码

[英]Compiling C Source for iOS

I have some existing source code that is written in C that I want to build and include in my iOS project. 我有一些用C编写的现有源代码,我想构建并包含在我的iOS项目中。 The entire source package is very large and is built using existing Makefiles and GCC. 整个源包非常大,使用现有的Makefile和GCC构建。 It is producing static libraries (.a files) that I would love to move over to my iOS project. 它正在生成静态库(.a文件),我很乐意转移到我的iOS项目。 However, the static libraries the Makefile produces is for x86 processors, which obviously won't work on iOS. 但是,Makefile生成的静态库是针对x86处理器的,这显然不适用于iOS。

Is there a way I can switch GCC to build for ARMv7/ARM64 instead, without making changes to the existing source (in most cases)? 有没有办法可以切换GCC来构建ARMv7 / ARM64, 而无需更改现有源 (在大多数情况下)? I know there is the -march switch for GCC or you can download ARM specific GCC compilers, so I know the general concept of building for a different architecture than the build machine. 我知道有GCC的-march开关,或者你可以下载ARM特定的GCC编译器,所以我知道构建不同于构建机器的架构的一般概念。

To build for ARM on Mac OS, will I have to download a different GCC compiler or is that capability built into the default GCC? 要在Mac OS上构建ARM,我是否必须下载不同的GCC编译器或者是否内置于默认GCC中?

I'm sorry for the lack of understanding of basic concepts here; 我很遗憾这里对基本概念缺乏了解; I'm primarily a Java and Objective-C developer, so building source for different architectures is a mostly foreign concept to me. 我主要是Java和Objective-C开发人员,因此构建不同架构的源代码对我来说是一个主要的外国概念。

Whilst GCC supports a good many CPU architecture and platforms, it is usually built for a single one. 虽然GCC支持许多CPU架构和平台,但它通常是为单个架构和平台构建的。 To compile for ARM you generally need an ARM-cross-compiling GCC targeted appropriately. 要为ARM编译,通常需要适当地针对ARM交叉编译GCC。

The default system compiler for MacOSX and iOS for all architectures is clang and has been for some time (the last version of GCC apple shipped in dev tools is creaking and obsolete, and definitely won't support ARMv8). 默认的系统编译器的MacOSX和iOS对所有架构的clang和已经有一段时间(出货开发工具GCC苹果的最后一个版本是吱吱作响的和过时的,肯定不会支持ARMv8)。

The usual way of getting clang is to install Xcode (free from the App Store). 获得clang的常用方法是安装Xcode(从App Store免费)。 There's a option in the installer (and in the UI of Xcode) to install the command-line tool package. 安装程序(以及Xcode的UI)中有一个选项可以安装命令行工具包。 This installs sym-links in /usr/bin to the compiler, and installs a bunch of other stuff you might expect such as make . 这会将/usr/bin中的sym-links安装到编译器中,并安装一些您可能期望的其他内容,例如make

clang is (mostly) command-line compatible with gcc , and furthermore, you'll find that if you run gcc from the command-line on a Mac with dev-tools installed, you in fact get clang. clang (大多数)命令行与gcc兼容,而且,你会发现如果你在安装了dev-tools的Mac上从命令行运行gcc ,你实际上就会得到clang。

$ gcc --version
Configured with: --    prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/usr/include/c++/4.2.1
Apple LLVM version 6.0 (clang-600.0.54) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin14.0.0
Thread model: posix 

clang comes with ARMv7, ARMv8, i686, x86_64 on MacOSX, and can be configured to compile for any of these from the command line (See documentation ) clang在MacOSX上附带ARMv7,ARMv8,i686,x86_64,并且可以配置为从命令行编译任何这些(参见文档

Given the above, there's a fair chance your code will compile with minimal changes to compiler-flags using the existing makefile. 鉴于上述情况,您的代码很可能会使用现有的makefile对编译器标志进行最小的更改。 You might want to read the documentation for lipo - which allows you to produce multi-architecture binaries. 您可能希望阅读lipo的文档 - 这允许您生成多架构二进制文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM