Ocelot
Ocelot

Reputation: 1753

compile and execute C program in iPhone

I have a C program which uses ASL library to get the logs of apps on the iPhone. Now, i have compiled this on Mac and it runs fine, fetching the logs of the apps on mac.. When i scp the compiled file to iPhone and try executing it, it says 'bad cpu type in executable'. So, i installed GNU C Compiler on the iPhone and compiled it.. now, when i compile it, it shows errors like this:

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

Please suggest what i need to do in order to get a c file compiled and run on the iPhone.

Upvotes: 0

Views: 3232

Answers (2)

user246672
user246672

Reputation:

On the command-line, this will compile an unsigned universal executable for iOS:

(So it will only execute on a real device that has been jailbroken, or launched somehow from a signed app (probably using calls that wouldn't be allowed in the 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

Given foo.c:

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

Next, scp it to your JB'ed device (scp foo mobile@name_of_iphone.local:.):

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

But you probably want to turn this into a trivial wrapper script like so, or use xcodebuild infrastructure instead:

~/bin/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 \
  "$@"

Credit

Other answers and http://clang-developers.42468.n3.nabble.com/targeting-iOS-tp4028940p4028942.html

Upvotes: 1

Stephane Delcroix
Stephane Delcroix

Reputation: 16222

on the simulator, and on the mac, you can run i386 code. But you can't on the real device as it's a different processor.

you have to compile your code as a static library targeting the iOS SDK from XCode. the commandline option for xcodebuild is -target iphoneos -arch armv7

Upvotes: 2

Related Questions