You cannot avoid creating an instance of Objective-C at runtime, but apparently you already have one of them.
If you want to interact with the C runtime, you can use the Objective-C API as described in the Objective-C Runtime Programming Guide and Objective-C Runtime Reference .
The idea is something like this (untested):
#include <objc/runtime.h> #include <objc/objc-runtime.h> id allocAndInitAutoreleasePool() { Class NSAutoreleasePoolClass = objc_getClass("NSAutoreleasePool"); id pool = class_createInstance(NSAutoreleasePoolClass, 0); return objc_msgSend(pool, "init"); } void drainAutoreleasePool(id pool) { (void)objc_msgSend(pool, "drain"); }
If you want to call these functions from another file, you will of course need to include objc / runtime.h. Or, alternatively, you can use id for void * in return from allocAndInit function and accept void * and drop back to id in drain function. (You can also format-declare struct objc_object and typedef struct objc_object * id, but I believe that this is not a guaranteed correct definition.)
You do not need to pass the -lobjc command to your link.
Needless to say, it probably works less, just to have your build scripts process .m files.
abarnert
source share