SYSTEM HALT

General Technology Blog

Compile C applications with gcc on iOS 4 (iPhone)

I search a lot on internet how to do this, and I found some info from Anastas Stoyanovsky. Most people posted that if you install libgcc on iOS 4 it will crash at boot but this is not true.

Here is what you need to do in order to be able to compile and run a C application on iphone:

1. You will need to Jailbreak the iPhone first, search on google for more info.
2. Install OpenSSH from Cydia.
3. Connect iPhone to your wireless network and SSH to it.
4. Download this application using: wget http://www.syshalt.net/pub/iphone/gcc-iphone/fake-libgcc_1.0_iphoneos-arm.deb
5. Install libcc using: dpkg –i fake-libgcc_1.0_iphoneos-arm.deb
6. Install iphone-gcc using this command: apt-get install iphone-gcc
(you can download this version from my website: iphone-gcc if does not work on your device the one that is installed by default)
7. Download using: wget http://www.syshalt.net/iphone/gcc-iphone/sdk-2.0-headers.tar.gz
8. Untar with command: tar -xvzf sdk-2.0-headers.tar.gz
9. Enter in the new created folder with: cd include-2.0-sdk-ready-for-iphone
10. Copy all files to include folder with command: cp –r * /usr/include
11. Now type: cd .. in order to return to the previous folder
12. Download using: wget http://www.syshalt.net/iphone/gcc-iphone/gcc_files.tar.gz
13. Untar with command: tar -xvzf gcc_files.tar.gz
14. Enter in the new created folder with command: cd gcc_files
15. Copy all files to /usr/lib using command: cp –r * /usr/lib
16. Install ldid to sign the application (this will prevent iOS to kill the application at startup) using: apt-get install ldid
17. Sign your compilet aplication using: ldid –S <application>
18. Run the application using: ./<application>

Update:
For iOS 7.x if you get errors installing gcc, try this patched version:

4.1 wget http://www.syshalt.net/iphone/gcc-iphone/iphone-gcc_4.2-20080604-1-8p_iphoneos-arm.deb
4.2 dpkg -i iphone-gcc_4.2-20080604-1-8p_iphoneos-arm.deb

Suggestion: Install using: apt-get install nano
This will help you code easy directly from terminal.

Here is an example of hello world using C and gcc to compile on iPhone iOS 4:

I used nano hello.c in terminal to open a new files and writed this simple application:

#include <stdio.h>
int main()
{
printf(“hello, world\n”);
return 0;
}

and then CTRL+X to save it, then I typed: gcc –o hello hello.c and after that ldid –S hello
Now you can run your application using ./hello

gcc

92 thoughts on “Compile C applications with gcc on iOS 4 (iPhone)

  1. Hello Sergiu.
    Thanks a lot for your instructions on compiling C apps on iOS 4. Before I found your site, I had spent so much time on this issue without success. Great help probably not only for me, but for all those in the community who get to know that compiling on iPhone 4 is much more pleasure than on the weaker earlier models.

    Hm, well… may I nevertheless dare to ask for an additional thing?: Do you know about anybody working on something similar for C++? At least, all my efforts end up in linker errors indicating libraries being missing.

  2. Thanks for your instructions,
    I follow your instructions to install without any error,
    but when i compile hello.c shows errors as follow…..
    Would you please help me?

    Thanks a lot

    xJames-i4:~ root# gcc -o hello hello.c

    In file included from /usr/include/stdio.h:64,
    from hello.c:1:
    /usr/include/_types.h:27:24: error: sys/_types.h: No such file or directory
    In file included from hello.c:1:
    /usr/include/stdio.h:70: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘va_list’
    /usr/include/stdio.h:75: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘off_t’
    /usr/include/stdio.h:80: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘size_t’
    /usr/include/stdio.h:87: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘fpos_t’
    /usr/include/stdio.h:145: error: expected specifier-qualifier-list before ‘fpos_t’
    /usr/include/stdio.h:171: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘extern’
    /usr/include/stdio.h:248: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘__BEGIN_DECLS’
    /usr/include/stdio.h:255: error: expected declaration specifiers or ‘…’ before ‘fpos_t’
    /usr/include/stdio.h: In function ‘fprintf’:
    /usr/include/stdio.h:258: error: expected declaration specifiers before ‘__DARWIN_LDBL_COMPAT’
    /usr/include/stdio.h:260: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘__DARWIN_ALIAS’
    /usr/include/stdio.h:261: error: expected declaration specifiers before ‘size_t’
    /usr/include/stdio.h:263: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘__DARWIN_ALIAS’
    /usr/include/stdio.h:264: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘__DARWIN_LDBL_COMPAT’
    /usr/include/stdio.h:266: error: expected ‘;’, ‘,’ or ‘)’ before ‘*’ token
    /usr/include/stdio.h:268: error: expected declaration specifiers before ‘size_t’
    /usr/include/stdio.h:273: error: storage class specified for parameter ‘sys_nerr’
    /usr/include/stdio.h:274: error: storage class specified for parameter ‘sys_errlist’
    /usr/include/stdio.h:277: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘__DARWIN_LDBL_COMPAT’
    /usr/include/stdio.h:284: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘__DARWIN_LDBL_COMPAT’
    /usr/include/stdio.h:286: error: expected declaration specifiers or ‘…’ before ‘size_t’
    /usr/include/stdio.h:287: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘__DARWIN_LDBL_COMPAT’
    /usr/include/stdio.h:288: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘__DARWIN_LDBL_COMPAT’
    /usr/include/stdio.h:292: error: expected declaration specifiers or ‘…’ before ‘va_list’
    /usr/include/stdio.h:292: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘__DARWIN_LDBL_COMPAT’
    /usr/include/stdio.h:293: error: expected declaration specifiers or ‘…’ before ‘va_list’
    /usr/include/stdio.h:293: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘__DARWIN_LDBL_COMPAT’
    /usr/include/stdio.h:294: error: expected declaration specifiers or ‘…’ before ‘va_list’
    /usr/include/stdio.h:294: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘__DARWIN_LDBL_COMPAT’
    /usr/include/stdio.h:296: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘__DARWIN_LDBL_COMPAT’
    /usr/include/stdio.h:297: error: expected declaration specifiers or ‘…’ before ‘va_list’
    /usr/include/stdio.h:297: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘__DARWIN_LDBL_COMPAT’
    /usr/include/stdio.h:299: error: expected declaration specifiers before ‘__END_DECLS’
    /usr/include/stdio.h:314: error: expected declaration specifiers or ‘…’ before ‘size_t’
    /usr/include/stdio.h:323: error: expected declaration specifiers or ‘…’ before ‘off_t’
    /usr/include/stdio.h:324: error: expected declaration specifiers before ‘off_t’
    /usr/include/stdio.h:341: error: expected declaration specifiers or ‘…’ before ‘size_t’
    /usr/include/stdio.h:341: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘__DARWIN_LDBL_COMPAT’
    /usr/include/stdio.h:342: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘__DARWIN_ALIAS’
    /usr/include/stdio.h:343: error: expected declaration specifiers or ‘…’ before ‘va_list’
    /usr/include/stdio.h:343: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘__DARWIN_LDBL_COMPAT’
    /usr/include/stdio.h:344: error: expected declaration specifiers or ‘…’ before ‘va_list’
    /usr/include/stdio.h:344: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘__DARWIN_LDBL_COMPAT’
    /usr/include/stdio.h:345: error: expected declaration specifiers or ‘…’ before ‘size_t’
    /usr/include/stdio.h:345: error: expected declaration specifiers or ‘…’ before ‘va_list’
    /usr/include/stdio.h:345: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘__DARWIN_LDBL_COMPAT’
    /usr/include/stdio.h:346: error: expected declaration specifiers or ‘…’ before ‘va_list’
    /usr/include/stdio.h:346: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘__DARWIN_LDBL_COMPAT’
    /usr/include/stdio.h:350: error: expected declaration specifiers before ‘__END_DECLS’
    /usr/include/stdio.h:362: error: expected declaration specifiers before ‘__END_DECLS’
    /usr/include/stdio.h:373: error: expected declaration specifiers or ‘…’ before ‘va_list’
    /usr/include/stdio.h:373: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘__DARWIN_LDBL_COMPAT’
    /usr/include/stdio.h:375: error: expected declaration specifiers before ‘__END_DECLS’
    hello.c:3: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{‘ token
    hello.c:6: error: old-style parameter declarations in prototyped function definition
    /usr/include/stdio.h:258: error: parameter name omitted
    /usr/include/stdio.h:258: error: parameter name omitted
    hello.c:6: error: expected ‘{‘ at end of input

  3. Good guide I like ur style. Fed up of people asking how it’s done about time sum1 took the masses through it step by step. Now all we need is to come up with a way of developing a step by step guide to creating new programs lol. And put Microsoft apple and all those other software theefs out of business. Every 1 use unix Linux and say no to close source inferia bs.

    but keep it legal 😉
    Peace
    Salaam
    Shalom

  4. @James: How you fixed the error (Ive done the hole process 2 times and it still gives me the same errors when compiling…)

    In file included from /usr/include/stdio.h:64,
    from hello.c:1:
    /usr/include/_types.h:27:24: error: sys/_types.h: No such file or directory
    In file included from hello.c:1:
    /usr/include/stdio.h:70: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘va_list’
    /usr/include/stdio.h:75: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’

    Thanks

  5. was missing line “9. Copy all files to include folder with command: cp –r * /usr/include”

  6. Thanks thanks thanks…!i’ve been looking for that trick since i bought that bad phone but now worth it, just one thing….you’re one hell of C bloogger this’s Great now i can C

  7. I agree, very well done post. A couple things I encountered —

    I had to “chmod” all the directories under /usr/include to 755 to allow them to be read by the compiler:
    find /usr/include -follow -type d -exec chmod 755 {} \;
    Maybe because I installed everything as root and compiled as mobile?

    I didn’t have to “ldid -S” in order to run the resulting hello command.

    I noticed several times that being in cydia on my iPad at the same time I tried to install something from the command line would interfere with the install. I got errors like this:
    Rons-iPad:~ root# apt-get install ldid
    E: Could not get lock /var/lib/dpkg/lock – open (35: Resource temporarily unavailable)
    E: Unable to lock the administration directory (/var/lib/dpkg/), is another process using it?

    Hopefully this will help someone else avoid these errors.

    Cheers!

  8. gobsmack, Cydia uses dpkg so you wont be able to use apt-get as long as the application is opened. cydia is slow, so if you want to keep you applications updated just use apt-get update and then apt-get upgrade in order to keep applications up to date (those who were installed with cydia)

  9. Thanks for the steps. Just two questions:
    (1) I use Ubuntu and Windows 7. I do not have a Macbook. Does your example require Macbook to work?
    (2) I cannot find gcc on Cydia. I press on Cydia icon. Then I see “Home – Cydia” screen. The information displayed at the bottom says: iPhone 4, iOS 4.1, Cydia 1.0.3222-73
    I then press “Search”. I key in “GCC”, “GNU”, “Compiler”, “iphone gcc”, “iphone-gcc” and nothing gives me gcc. How do I install gcc on Cydia if it is not listed?

    Thanks

  10. hi there,

    please follow exactly the guide:

    1. your phone needs to be jailbreaked, and you can use guide both on windows, linux, mac.
    2. u need to jailbreak the iphone then install openssh from cydia.
    3. read guide careful:
    —- Connect iPhone to your wireless network and SSH to it (using putty or any other ssh client).
    —- Download this application (from terminal) using: wget http://www.syshalt.net/iphone/gcc-iphone/fake-libgcc_1.0_iphoneos-arm.deb
    —- Install libcc using command: dpkg –i fake-libgcc_1.0_iphoneos-arm.deb
    —- “Install gcc from Cydia with command (type this command in terminal): apt-get install iphone-gcc”

  11. Anybody managed to compile C++ with that trick ?
    I always get a ‘error: iostream: No such file or directory’ error…

  12. @Jeremy

    1) Mac is not necessary. This is just so your iPhone can compile c programs by itself.

    2) Go to Cydia→Manage→Settings and make sure it says “Developer.”

  13. how can I remove fake-libgcc_1.0_iphoneos-arm.deb? Something went “south” while installing it and I want to back it out

  14. Thanks Sergiu, that’s what I did tried but it gives me no error messages and it does nothing. I must have hosed something.. I’ll have to restore… oh well.

  15. Thanks for your specific tutorial!
    But I have a problem on my own device.
    I follow all your steps to No.15. I cannot find the command “gcc” with “which gcc”in my ipod touch (iOS 4.1). And I also check several times that iphone-gcc has installed correctly.
    Could you help me with that?

  16. I have found the solution.
    With command “apt-get install iphone-gcc”, the package ” iphone-gcc_4.2-20090304-1-9_iphoneos-arm.deb” will be installed right now. But there is a bug in it. Install the previous version will solve it. Type the command below:
    wget http://apt.saurik.com/debs/iphone-gcc_4.2-20080604-1-8_iphoneos-arm.deb
    dpkg -i iphone-gcc_4.2-20080604-1-8_iphoneos-arm.deb

    You’d better add this to your article in case someone else has the same problem.
    Thanks for your tutorial!

  17. For those interested in g++: If you have access to the apple SDK you can find the headers in the /Developers directory. You can just copy the contents of the C++ directory to /usr/include/c++. Then also copy the libstdc++.dylib to /usr/lib, the g++ works.

  18. Will gnu gcc 4.6 from the gcc site work or is that for
    Pc another desktop is? Igot all this on my iphone4,
    Igot mobile terminal up and running just need the
    Compiler now is this the only way to do it?

  19. Thanks! I’ve been banging my head against the wall and looking for a couple days for a way to install libgcc so I could get gcc running and this worked like a charm!

  20. hello, for the single c file ok, but if i want to use make command don’t go 🙁

  21. It works great!
    Thanks you very very much!

    I had to manage some privilegies before compile to avoid use superuser every compilation:
    chmod -R 755 /usr/include
    chmod -R 777 /patch/of/sourcecode

  22. THANK YOU SO MUCH!!!!!!! You fixed every error that I had with this one easy to follow tutorial thank you

  23. An excellent post that gets straight to the point.

    I love the fact that you can compile C for the iOS devices. Being a software dev trained in C/C++, I didn’t want to have to learn a new language (Objective C) just to port a few apps to iOS 4.

    Thank you the information.

  24. I have error while trying to compile g++

    ld warning: in /usr/lib/libstdc++.dylib, missing required architecture arm in file

    I have copied all c++ from SDK and the libstdc++.6.0.9, still not working

  25. Does anyone have the relevant g++ files? I’m trying to compile v8, but don’t have access to the sdk
    – thanks

  26. hi, this is a great tutorial.
    now i can compile a hello world program on my iphone 3g.
    thanx a lot, man.

  27. Hi,

    nice work..

    could you please help me .. I’m trying to install hping3 … and getting following errors:

    ./configure
    build byteorder.c…
    create byteorder.h…
    ./configure: line 81: -: command not found
    ==> WARNING: no Tcl header files found!
    ————————————–
    system type: DARWIN

    LIBPCAP : PCAP=-lpcap
    PCAP_INCLUDE :
    MANPATH : /usr/local/man
    USE_TCL :
    TCL_VER :
    TCL_INC :
    LIBTCL : -ltcl -lm -lpthread
    TCLSH :

    (to modify try configure –help)
    ————————————–
    creating Makefile…
    creating dependences…
    In file included from ars.h:20,
    from apd.c:19:
    bytesex.h:22:3: error: #error can not find the byte order for this architecture, fix bytesex.h
    In file included from apd.c:19:
    ars.h:190:2: error: #error “Please, edit Makefile and add -DBYTE_ORDER_(BIG|LITTLE)_ENDIAN”
    ars.h:254:2: error: #error “Please, edit Makefile and add -DBYTE_ORDER_(BIG|LITTLE)_ENDIAN”
    ars.h:323:2: error: #error “Please, edit Makefile and add -DBYTE_ORDER_(BIG|LITTLE)_ENDIAN”
    In file included from ars.h:20,
    from ars.c:24:
    bytesex.h:22:3: error: #error can not find the byte order for this architecture, fix bytesex.h
    In file included from ars.c:24:
    ars.h:190:2: error: #error “Please, edit Makefile and add -DBYTE_ORDER_(BIG|LITTLE)_ENDIAN”
    ars.h:254:2: error: #error “Please, edit Makefile and add -DBYTE_ORDER_(BIG|LITTLE)_ENDIAN”
    ars.h:323:2: error: #error “Please, edit Makefile and add -DBYTE_ORDER_(BIG|LITTLE)_ENDIAN”
    In file included from ars.h:20,
    from arsglue.c:7:
    bytesex.h:22:3: error: #error can not find the byte order for this architecture, fix bytesex.h
    In file included from arsglue.c:7:
    ars.h:190:2: error: #error “Please, edit Makefile and add -DBYTE_ORDER_(BIG|LITTLE)_ENDIAN”
    ars.h:254:2: error: #error “Please, edit Makefile and add -DBYTE_ORDER_(BIG|LITTLE)_ENDIAN”
    ars.h:323:2: error: #error “Please, edit Makefile and add -DBYTE_ORDER_(BIG|LITTLE)_ENDIAN”
    In file included from ars.h:20,
    from rapd.c:11:
    bytesex.h:22:3: error: #error can not find the byte order for this architecture, fix bytesex.h
    In file included from rapd.c:11:
    ars.h:190:2: error: #error “Please, edit Makefile and add -DBYTE_ORDER_(BIG|LITTLE)_ENDIAN”
    ars.h:254:2: error: #error “Please, edit Makefile and add -DBYTE_ORDER_(BIG|LITTLE)_ENDIAN”
    ars.h:323:2: error: #error “Please, edit Makefile and add -DBYTE_ORDER_(BIG|LITTLE)_ENDIAN”
    In file included from ars.h:20,
    from split.c:11:
    bytesex.h:22:3: error: #error can not find the byte order for this architecture, fix bytesex.h
    In file included from split.c:11:
    ars.h:190:2: error: #error “Please, edit Makefile and add -DBYTE_ORDER_(BIG|LITTLE)_ENDIAN”
    ars.h:254:2: error: #error “Please, edit Makefile and add -DBYTE_ORDER_(BIG|LITTLE)_ENDIAN”
    ars.h:323:2: error: #error “Please, edit Makefile and add -DBYTE_ORDER_(BIG|LITTLE)_ENDIAN”
    now you can try `make’

  28. hi.

    i just run into a problem, while trying to get gcc on my ipad 2.

    i followed the steps you provide, directly using iSSH on my ipad.

    everything went fine (except an error dpkg-ing the iphone-gcc, saying gcc was left unconfigured – but i don’t think this is the problem).

    the problem : when i try to compile the simple hello.c program, it says
    ” ld: library not found for -lcrt1.10.5.o
    collect2 : ld returned 1 exit status ”

    Can you point me the right direction?

    Thank you

  29. Thank you for the response.

    Yeah, actually that was the problem (gcc left unconfigured). Reinstalled and now everything works fine.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.