C#代码:(windows)
Assembly assembly = Assembly.LoadFrom(AssemblyPath);
System.Type[] objTypes = assembly.GetTypes();
Type libType = null;
我想在Mac上实现同样的功能,其中AssemblyPath是静态库(libTEST.a)或dylib文件的路径。在Objective-C(Mac)中可以吗?我尝试使用NSBundle。但是我想要一些好的解决方案。
首先,这与Xcode完全无关。现在,您不能动态加载静态库,因为静态库只是对象文件的集合,它们本身不是可执行文件。
为了加载动态库,请使用dlopen()
API:
void *handle = dlopen("/path/to/library.dylib", RTLD_LAZY);
获得C函数指针:
int (*computeAnswer)(void) = dlsym(handle, "ComputeAnswer");
int answer = computeAnswer(); // 42
获得不具有extern "C"
链接的C ++函数指针(名称混杂):
int (*mangledFunction)(void) = dlsym(handle, "i$mangledFunction_@v");
您甚至可以通过编译器的Objective-C命名约定来入侵自己:linker
@class MyShinyClass;
Class cls = dlsym(handle, "OBJC_CLASS_$_MyShinyClass");
MyShinyClass *instance = [[cls alloc] init];
使用完库后,将其丢弃:
dlclose(handle);