在iPhone中编译并执行C程序

时间:2013-03-19 11:00:54

标签: iphone ios c compilation

我有一个C程序,它使用ASL库来获取iPhone上的应用程序日志。现在,我已经在Mac上编译了它并运行正常,在mac上获取应用程序的日志。当我将编译后的文件scp到iPhone并尝试执行它时,它会说“可执行文件中的错误cpu类型”。所以,我在iPhone上安装了GNU C Compiler并对其进行了编译..现在,当我编译它时,它会显示如下错误:

cP-iphone:~ root# gcc test.c
test.c:1:19: error: stdio.h: No such file or directory
test.c:2:17: error: asl.h: No such file or directory
test.c: In function 'main':
test.c:5: warning: incompatible implicit declaration of built-in function 'printf'
test.c:16: error: 'aslmsg' undeclared (first use in this function)
test.c:16: error: (Each undeclared identifier is reported only once
test.c:16: error: for each function it appears in.)
test.c:16: error: expected ';' before 'q'
test.c:21: error: 'q' undeclared (first use in this function)
test.c:21: error: 'ASL_TYPE_QUERY' undeclared (first use in this function)
test.c:22: error: 'ASL_KEY_SENDER' undeclared (first use in this function)
test.c:22: error: 'ASL_QUERY_OP_EQUAL' undeclared (first use in this function)
test.c:23: error: 'aslresponse' undeclared (first use in this function)
test.c:23: error: expected ';' before 'r'
test.c:24: error: 'NULL' undeclared (first use in this function)
test.c:24: error: 'm' undeclared (first use in this function)
test.c:24: error: 'r' undeclared (first use in this function)
test.c:27: warning: assignment makes pointer from integer without a cast
test.c:30: warning: assignment makes pointer from integer without a cast

请建议我需要做什么才能获得编译并在iPhone上运行的c文件。

2 个答案:

答案 0 :(得分:2)

在模拟器上,在mac上,你可以运行i386代码。但你不能在真实的设备上,因为它是一个不同的处理器。

您必须将代码编译为从静态库。 xcodebuild的命令行选项为-target iphoneos -arch armv7

答案 1 :(得分:0)

在命令行中,这将为iOS编译 unsigned 通用可执行文件:

(因此它只会在已经越狱的真实设备上执行,或者从已签名的应用程序以某种方式启动(可能使用IAS中不允许的调用)。)

clang -isysroot "$(xcode-select -p)/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk" \
  -arch armv7 \
  -arch armv7s \
  -arch arm64 \
  -mios-version-min=5.0 \
  foo.c -o foo

鉴于foo.c

#include <stdio.h>
int main(void){printf("Hello, world!\n");return(0);}

接下来,scp到你的JB'ed设备(scp foo mobile@name_of_iphone.local:.):

MBF:~ mobile$ ./foo
Hello, world!
MBF:~ mobile$ rm foo
MBF:~ mobile$ ^D # logout

但是你可能想把它变成一个像这样的简单包装脚本,或者改用xcodebuild基础结构:

〜/斌/ clang_ios

#!/bin/sh
set -e
exec clang -isysroot "$(xcode-select -p)/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk" \
  -arch armv7 \
  -arch armv7s \
  -arch arm64 \
  -mios-version-min=5.0 \
  "$@"

信用

其他答案和http://clang-developers.42468.n3.nabble.com/targeting-iOS-tp4028940p4028942.html

相关问题