Xcode compile console application - c programming

Is it possible to use Xcode with the iPhone 3.1.3 SDK to compile a standard C program that will work on the iphone? I am trying to use xcode to compile the basic “hello world”, but to no avail. Do I need to get an additional application template, or am I just missing something?

+4
source share
4 answers

If you use the boiler plate template for a cocoa application that uses NSApplicationMain and several other structures needed to run the cocoa program, then you can start writing C methods without pressing objective-c. Cautions:

1) for testing purposes, it looks like this: when using xcode, it is best to start with the "Window-Based Application" template offered in the category of new iphone projects. This is a minimal template without an interface - just a window.

2) In iphone there is no "main ()" persay. You must put your code in the file "AppDelegate.m", which will actually be "[YourProjectName] AppDelegate.m". 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 the C functions you wrote, which you can do either in this source file, or, better, in a separate file, which you #import. Please note that the application will be blocked until the completion of all your c-code.

3) No printf, I do not believe it. I'm sorry. One way to get information is to use NSLog, but expects objective-c lines to not contain C lines. Therefore, if you want to see any status from your C program, you only have to use the small objective-c bit. Corresponding line:

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

where it converts your C-string to objective-c, which NSLog will successfully print to the console (visible using the console application in the Utility folder in OSX-based applications).

It's good?

+2
source

If you have a jailbroken iPhone, install GCC and other requirements (see this page http://antirez.com/page/iphone-gcc-guide.html ). Compile code on device using 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 that the compiled application is a native console application and does not support the interface, so it can only be run from the terminal.

+2
source

iPhone does not officially support console applications.

0
source

I'm sure this is probably a meek answer, but you want to check out: "Programming on the iPhone: A Guide for the Big Senior Ranch." I myself worked on this book, and it was 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 do not mention whether you want to run this program on iPhone / iPad / iPod Touch.

If you are just learning Cocoa and don’t want it running on iPhone, check out this sister book: 'Cocoa ® Programming for Mac® OS X (3rd Edition)'.

0
source

Source: https://habr.com/ru/post/1313044/


All Articles