How to load dylib or .a (static library) file using objective-C?
First off, this has precisely nothing to do with Xcode.
Now, you can't load static libraries dynamically, because a static library is just a collection of object files, which are not, by themselves, executable.
In order to load a dynamic library, use the dlopen()
API:
void *handle = dlopen("/path/to/library.dylib", RTLD_LAZY);
To get a C function pointer:
int (*computeAnswer)(void) = dlsym(handle, "ComputeAnswer");
int answer = computeAnswer(); // 42
To get a C++ function pointer without extern "C"
linkage (mangled name):
int (*mangledFunction)(void) = dlsym(handle, "i$mangledFunction_@v");
You can even hack yourself through the Objective-C naming convention of the linker compiler:
@class MyShinyClass;
Class cls = dlsym(handle, "OBJC_CLASS_$_MyShinyClass");
MyShinyClass *instance = [[cls alloc] init];
When you're done with the library, dispose of it:
dlclose(handle);