Coming from a server hacker's point of view, I thought, what better way to do this than to do everything from the command line. And if I was going to do this for all my examples, I might as well create a reusable makefile for this!
Below, we have a setup with a makefile, a main file, and a objective-c header and implementation source file. The makefile will compile objective-c modules and link them with the main source file to create a "hello" executable.
The makefile:
CC = clang MAIN = hello SRCS = main.o Test.o default: $(MAIN) $(MAIN): $(SRCS) $(CC) -O0 -Wall -o $(MAIN) $(SRCS) -framework Foundation clean: find . -name "*.o" -exec rm {} \; if [ -f "$(MAIN)" ]; then rm $(MAIN); fi all: default
Test.h
#import <Foundation/NSObject.h> @interface Contrived : NSObject { } - (void)log:(NSString *)arg1; @end
Test.m
#import "Test.h" #import <Foundation/Foundation.h> @implementation Contrived - (void)log:(NSString *)arg1 { NSLog(@"From Contrived.log: %@", arg1); } @end
main.m
#import "Test.h" #import <Foundation/Foundation.h> int main (void) { NSAutoreleasePool *pool = [NSAutoreleasePool new]; id simpleObj = [Contrived alloc]; [simpleObj log:@"Hello World!"]; [simpleObj release]; [pool release]; return 0; }
A couple of notes about the makefile:
CC=clang
This specify the compiler/linker. You can specify alternatives such as gcc. But Clang generally provides cleaner error messages which is important for working on test snippets.
SRCS = main.o Test.o
This allows you to specify additional modules to compile (other than the main function) here, separated by a single space. For example, if you want to another module with the files "MyLibrary.h" and "MyLibrary.m", then your SRCS declaration would be:
SRCS = main.o Test.o MyLibrary.oThat's it. Get coding!
No comments:
Post a Comment