Objective-C检查是否定义了结构 [英] Objective-C Check if Structs is defined
问题描述
我的iOS应用程序可以使用可选的外部第三方库.
My iOS application can use an optional external 3rd party library.
我考虑使用此答案(弱链接-检查类是否存在并使用该类),并在执行特定于该库的代码之前检测该类是否存在.
I thought of using this answer (Weak Linking - check if a class exists and use that class) and detect if the class exists before executing code specific to this library.
但是,我发现此外部库不是作为Objective-C类编写的,而是作为C STRUTS和函数编写的.
However, I found out that this external library is not written as Objective-C classes, but rather as C STRUTS and functions.
是否有类似的技术可以让我检查C Strut或函数是否存在?还是一些更好的选择,以查看运行时该库是否存在?
Is there a similar technique that would allow me to check if a C Strut or function exists? Or some better alternative to see if this library is present at runtime?
推荐答案
struct
是编译时工件.他们告诉编译器如何布置内存区域.完成此操作后,struct
就不再需要了.与具有元数据的Objective-C类不同,structs
没有运行时状态.这就是为什么无法在运行时检测到它们的原因.
struct
s are compile-time artifacts. They tell the compiler how to lay out a region of memory. Once that is done, struct
s become unnecessary. Unlike Objective-C classes which have metadata, structs
have no runtime presence. That is why it is not possible to detect them at runtime.
You can check if a dynamic library is present by calling dlopen
, and passing its path:
void *hdl = dlopen(path_to_dl, RTLD_LAZY | RTLD_LOCAL);
if (hdl == NULL) {
// The library failed to load
char *err = dlerror(); // Get the error message
} else {
dlclose(hdl);
}
如果dlopen
返回NULL
,则无法加载库.您可以通过调用 dlerror
.您需要调用 dlclose
.
If dlopen
returns NULL
, the library cannot be loaded. You can get additional info by calling dlerror
. You need to call dlclose
after you are done.
这篇关于Objective-C检查是否定义了结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!