Here's a little example on how to compile Objective-C on the commandline, with a makefile. All code can be downloaded here: test cars.zip or copy/pasted from below.
Create a file called Makefile:
CC=clang LIBS=-framework Foundation
all: test_cars
test_cars: test_cars.m car.m $(CC) $(LIBS) test_cars.m car.m -o test_cars
clean: rm -f test_cars *.o Now create a header file:
#import <Cocoa/Cocoa.h> @interface Car : NSObject { float fillLevel; }
-(float) gasLevel; -(void) addGas; @end
Plus the implementation:
#import "Car.h" @implementation Car
-(id) init { self = [super init]; if (self) { fillLevel = 0.0; } return self; }
-(void) addGas { NSLog(@"Added one gallon!"); fillLevel += 1.0; }
-(float) gasLevel { return fillLevel; }
@end
And the file containing main:
#import <Foundation/Foundation.h> #import "car.h" int main(int argc, const char * argv[]) { @autoreleasepool { // insert code here... Car *c = [[Car alloc] init]; NSLog(@"Level = %f", [c gasLevel]); [c addGas]; NSLog(@"Level = %f", [c gasLevel]); } return 0; }
Type make in the directory containing the above files, and run the example with ./test_cars. Enjoy the commandline goodness.
The code in car.m and car.h is mostly by your friendly neighborhood Java/Grails developer.