Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Command line tool in Objective-C using Foundation?

is there a simple way to create a command-line tool in Objective C?

I'd rather not use XCode, because XCode has targets and executables, and just complicated stuff.

I'd like to go classic way, just create a Makefile, compile something get an executable, play with it.

--

If this is not possible, is there any way to run the executable I get from regular XCode CL project? It creates a build and again - complicated stuff.

I just want to use my terminal instead of XCode's Console.

like image 549
Vojto Avatar asked Oct 27 '10 05:10

Vojto


1 Answers

Yes. Just write your files as normal Objective-C files and compile with GCC or Clang, linking in the Foundation framework. It's hardly different from a normal C program.

Simple example:

chuck$ cat > main.m

#import <Foundation/Foundation.h>

int main() {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSArray *words = [NSArray arrayWithObjects:@"Hello,", @"world!", @"Check", @"this", @"out!", nil];
    NSLog(@"%@", [words componentsJoinedByString:@" "]);
    [pool release];
    return 0;
}

chuck$ cc -framework Foundation -o my-app main.m
chuck$ ./my-app
2010-10-26 22:32:04.652 my-app[5049:903] Hello, world! Check this out!
like image 116
Chuck Avatar answered Sep 24 '22 20:09

Chuck