简体   繁体   English

Objective-C检查是否定义了结构

[英]Objective-C Check if Structs is defined

My iOS application can use an optional external 3rd party library. 我的iOS应用程序可以使用可选的外部第3方库。

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. 我想到了使用此答案( 弱链接-检查一个类是否存在并使用该类 )并在执行特定于该库的代码之前检测该类是否存在。

However, I found out that this external library is not written as Objective-C classes, but rather as C STRUTS and functions. 但是,我发现此外部库不是作为Objective-C类编写的,而是作为C STRUTS和函数编写的。

Is there a similar technique that would allow me to check if a C Strut or function exists? 有没有类似的技术可以让我检查C Strut或函数是否存在? Or some better alternative to see if this library is present at runtime? 还是一些更好的替代方案,以查看运行时该库是否存在?

struct s are compile-time artifacts. struct是编译时的工件。 They tell the compiler how to lay out a region of memory. 他们告诉编译器如何布置内存区域。 Once that is done, struct s become unnecessary. 一旦完成,就不需要使用struct Unlike Objective-C classes which have metadata, structs have no runtime presence. 与具有元数据的Objective-C类不同, structs没有运行时状态。 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: 您可以通过调用dlopen并传递其路径来检查是否存在动态库:

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);
}

If dlopen returns NULL , the library cannot be loaded. 如果dlopen返回NULL ,则无法加载该库。 You can get additional info by calling dlerror . 您可以通过调用dlerror获得更多信息。 You need to call dlclose after you are done. 完成后,您需要调用dlclose

AFAIK a classical C function has to exist. AFAIK必须存在经典的C函数。 It is statically bound during the linking process and it is not, like Objective-C mehtods, dynamically bound on runtime. 它在链接过程中是静态绑定的,而不像Objective-C方法那样在运行时动态绑定。

So when the code compiles AND links without errors or warnings, then you should be fine. 因此,当代码在编译AND链接时没有错误或警告时,您就可以了。

The same for structs. 对于结构也是如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM