开发者

xcode compile console application - c programming

开发者 https://www.devze.com 2023-01-04 05:46 出处:网络
Is it possible to use Xcode using iPhone 3.1.3 SDK to compile a standard C program that will work on the iphone? I\'m trying to use xcode to compile a basic \'hello world\' but to no avail. Do I need

Is it possible to use Xcode using iPhone 3.1.3 SDK to compile a standard C program that will work on the iphone? I'm trying to use xcode to compile a basic 'hello world' but to no avail. Do I need to obtain an additional 开发者_StackOverflow中文版application template or am I just missing something?


If you use the boiler plate template for a cocoa application, which uses NSApplicationMain and the few other structures necessary to jumpstart a cocoa program, then you are free to start writing C methods without ever hitting objective-c. Caveats:

1) for testing purposes it looks like, when using xcode, your best bet is to start with the "Window-Based Application" template offered under the iphone category of new projects. It is a minimal template with no UI -- just a window.

2) There is no "main()" persay in an iphone. You have to place your code in the "AppDelegate.m" file which will actually be "[YourProjectName]AppDelegate.m". Inside here you will find a method:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
    //YOUR CODE GOES HERE

    return YES;
}

This is a good place to call C functions you've written, which you can do either in that source file, or better, in a separate file that you #import. Note that the application will block until all of your c-code finishes executing.

3) There is no printf, i don't believe. Sorry. One way to get info out is to use NSLog -- but that expects objective-c strings not C strings. So if you want to actually see any status out of your C program, you'll have to use just a tiny bit of objective-c. The line in question is:

char *your_char_pointer = //...gimme a string;
NSLog([NSString stringWithCString:your_char_pointer]);

where this will convert your C String into an Objective-C string that NSLog will happily print to the console (seen by using the console application in the Utility folder in Applications on OSX).

That good?


If you have a jailbroken iPhone then, install GCC, and the other requirments (see this page http://antirez.com/page/iphone-gcc-guide.html). Compile code on-device with gcc

#include <stdio.h>

int main(void)
{
  printf("Hello World\n");
  return 0;
}

Use Terminal to compile

gcc -c main.c -o hello.o

ld hello.o -e _main -o Hello

Remember, the compiled app is native console app and does not support UI so it can only be run from Terminal.


I'm sure this is probably a crud answer, but you my want to check out: 'iPhone Programming: The Big Nerd Ranch Guide'. I've worked through this book myself and it has been a great resource.

And you can certainly use XCode to compile a standard C program and output your 'Hello World' to the console. However, you don't mention whether or not you want to run this program on an iPhone/iPad/iPod Touch.

If you're just looking to learn Cocoa and don't want to necessarily run this on an iPhone, check out the sister book: 'Cocoa® Programming for Mac® OS X (3rd Edition)'.


iPhone does not officially support console applications.

0

精彩评论

暂无评论...
验证码 换一张
取 消