I’ve been looking at compiling up some code for OS X on the Mac. Some things that are a little more involved than just AppleScript. The developer tools from Apple are pretty nice, but for various reasons, I wanted to use a makefile since there is some C-code, and some other steps that I wanted to automate based on the dependencies.
Ultimately, I wanted to use the scripting bridge which means that I would have to use Objective-C at least, even if I don’t use the full Cocoa suite. I looked for a while, but there didn’t seem to be anything about using gcc/g++ for compiling objective-C at the command line. Actually, it turns out that the gcc that comes with the developer tools is what Xcode has underneath to compile the source code.
You can generate the object file just like you would with any C/C++ file. The following will generate an object file for linking written in Objective-C with the standard file suffix for the source code.
gcc -c src/test.m -o objs/test.o
The automatically recognized Objective-C filetypes by suffix are (see the gcc man page for more information):
file.m Objective-C source code. Note that you must link with the libobjc library to make an Objective-C program work. file.mi Objective-C source code which should not be preprocessed. file.mm file.M Objective-C++ source code. Note that you must link with the libobjc library to make an Objective-C++ program work. Note that .M refers to a literal capital M. file.mii Objective-C++ source code which should not be preprocessed.
If necessary, the file type is also accepted on the command line with the either the flags “-ObjC”/”-ObjC++” or the “-x” flag and one of the following:
objective-c objective-c-header objective-c-cpp-output objective-c++ objective-c++-header objective-c++-cpp-output
Gcc takes the following Objective-C and Objective-C++ options
Objective-C and Objective-C++ Language Options -fconstant-string-class=class-name -fgnu-runtime -fnext-runtime -fno-nil-receivers -fobjc-call-cxx-cdtors -fobjc-direct-dispatch -fobjc-sjlj-exceptions -fobjc-gc -freplace-objc-classes -fzero-link -gen-decls -Wassign-intercept -Wno-protocol -Wselector -Wno-property-assign-default -Wstrict-selector-match -Wundeclared-selector
If you need the frameworks (like Foundation or Cocoa), you will need to specify them during the linking step. The frameworks can be specified with the “-frameworks” option. The following will link the object file above with the Foundation and Scripting Bridge frameworks
gcc objs/test.o -framework Foundation -framework ScriptingBridge -o bin/test
You may need to specify the search directories for the frameworks with the “-F” option. See the gcc man page for more information.