为 iOS 编译 C 源代码 [英] Compiling C Source for iOS

查看:25
本文介绍了为 iOS 编译 C 源代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些用 C 语言编写的现有源代码,我想将其构建并包含在我的 iOS 项目中.整个源包非常大,是使用现有的 Makefile 和 GCC 构建的.它正在生成静态库(.a 文件),我很想将其转移到我的 iOS 项目中.但是,Makefile 生成的静态库是针对 x86 处理器的,这显然不适用于 iOS.

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.

有没有一种方法可以将 GCC 切换为针对 ARMv7/ARM64 进行构建,无需更改现有源(在大多数情况下)?我知道 GCC 有 -march 开关,或者您可以下载 ARM 特定的 GCC 编译器,所以我知道为与构建机器不同的架构构建的一般概念.

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.

要在 Mac OS 上为 ARM 构建,我是否必须下载不同的 GCC 编译器,或者该功能是否内置到默认 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?

抱歉这里对基本概念缺乏了解;我主要是一名 Java 和 Objective-C 开发人员,因此为不同的架构构建源代码对我来说几乎是一个陌生的概念.

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.

推荐答案

虽然 GCC 支持很多 CPU 架构和平台,但它通常是为单个构建的.要为 ARM 编译,您通常需要一个针对 ARM 的交叉编译 GCC.

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.

适用于所有架构的 MacOSX 和 iOS 的默认系统编译器是 clang 并且已经有一段时间了(开发工具中提供的 GCC 苹果的最后一个版本是吱吱作响和过时的,绝对不会支持ARMv8).

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).

获得 clang 的常用方法是安装 Xcode(从 App Store 免费).安装程序(以及 Xcode 的 UI)中有一个选项可以安装命令行工具包.这会将 /usr/bin 中的符号链接安装到编译器,并安装一堆您可能期望的其他东西,例如 make.

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(主要)与 gcc 的命令行兼容,此外,如果您从在安装了 dev-tools 的 Mac 上使用命令行,你实际上会听到 clang.

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 在 MacOSX 上随 ARMv7、ARMv8、i686、x86_64 一起提供,并且可以配置为从命令行编译其中任何一个(请参阅 文档)

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)

鉴于上述情况,您的代码很有可能在使用现有 makefile 对编译器标志进行最少更改的情况下进行编译.您可能想阅读 lipo 的文档 -它允许您生成多架构二进制文件.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆