编译iOS的C源代码 [英] Compiling C Source for iOS
问题描述
我有一些要用C编写的现有源代码,我想对其进行构建并将其包含在我的iOS项目中.整个源代码包非常大,并使用现有的Makefile和GCC构建.它正在生成静态库(.a文件),我希望将其移至我的iOS项目.但是,Makefile生成的静态库是针对x86处理器的,显然不能在iOS上运行.
是否有一种方法可以切换GCC为ARMv7/ARM64构建,在不更改现有源代码的情况下(在大多数情况下)?我知道有用于GCC的-march开关,或者您可以下载ARM特定的GCC编译器,所以我知道为与构建机器不同的体系结构进行构建的一般概念.
要在Mac OS上为ARM构建,我是否必须下载其他GCC编译器,或者该功能内置于默认GCC中?
很抱歉,我对这里的基本概念缺乏理解;我主要是Java和Objective-C开发人员,因此对我来说,构建不同体系结构的源代码主要是一个陌生的概念.
尽管GCC支持许多CPU架构和平台,但通常是为单个平台构建的.要为ARM进行编译,通常需要适当地针对ARM交叉编译GCC.
适用于所有体系结构的MacOSX和iOS的默认系统编译器是clang
,并且已经使用了一段时间(开发工具中提供的GCC苹果的最新版本令人讨厌且过时,并且肯定不支持ARMv8). /p>
获取clang
的通常方法是安装Xcode(可从App Store免费获得).安装程序(和Xcode的UI)中有一个选项可以安装命令行工具包.这会将/usr/bin
中的符号链接安装到编译器,并安装您可能期望的一堆其他内容,例如make
.
clang
(大多数情况下)与gcc
命令行兼容,此外,您会发现,如果您在装有dev-tools的Mac上从命令行运行gcc
,您实际上得到c声.
$ 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
在MacOSX上随ARMv7,ARMv8,i686和x86_64一起提供,并且可以配置为通过命令行针对其中的任何一个进行编译(请参见 lipo
的文档-该文档可让您生成多体系结构二进制文件.
I have some existing source code that is written in C that I want to build and include in my iOS project. The entire source package is very large and is built using existing Makefiles and GCC. It is producing static libraries (.a files) that I would love to move over to my iOS project. However, the static libraries the Makefile produces is for x86 processors, which obviously won't work on 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)? 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.
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?
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.
Whilst GCC supports a good many CPU architecture and platforms, it is usually built for a single one. To compile for ARM you generally need an ARM-cross-compiling GCC targeted appropriately.
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).
The usual way of getting clang
is to install Xcode (free from the App Store). There's a option in the installer (and in the UI of Xcode) to install the command-line tool package. This installs sym-links in /usr/bin
to the compiler, and installs a bunch of other stuff you might expect such as 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.
$ 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)
Given the above, there's a fair chance your code will compile with minimal changes to compiler-flags using the existing makefile. You might want to read the documentation for lipo
- which allows you to produce multi-architecture binaries.
这篇关于编译iOS的C源代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!