September 2002
Beginner to intermediate
216 pages
7h 43m
English
You can turn answer.o into a bundle, which can be dynamically loaded using the commands shown in Example 5-6.
cc -flat_namespace -bundle -undefined suppress \ -o libanswer.bundle answer.o
You do not need to specify the bundle at link time. Instead, use the
dyld functions
NSCreateObjectFileImageFromFile and
NSLinkModule to load the library. Then, you can
use NSLookupSymbolInModule and
NSAddressOfSymbol to access the symbols that the
library exports. Example 5-7 loads
libanswer.bundle and invokes the
get_answer function. Example 5-7
is similar to Example 5-4, but many lines (shown in
bold) have been added.
/* * deep_thought_dyld.c: Obtain the answer to life, the universe, * and everything, and act startled when you actually hear it. */ #include <stdio.h> #import <mach-o/dyld.h> int main( ) { int the_answer; int rc; // Success or failure result value NSObjectFileImage img; // Represents the bundle's object file NSModule handle; // Handle to the loaded bundle NSSymbol sym; // Represents a symbol in the bundle int (*get_answer) (void); // Function pointer for get_answer /* Get an object file for the bundle. */ rc = NSCreateObjectFileImageFromFile("libanswer.bundle", &img); if (rc != NSObjectFileImageSuccess) { fprintf(stderr, "Could not load libanswer.bundle.\n"); exit(-1); } /* Get a handle for the bundle. */ handle = NSLinkModule(img, ...