Objective-C Check if Structs is defined
Asked Answered
W

2

0

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.

However, I found out that this external library is not written as Objective-C classes, but rather as C STRUTS and functions.

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?

Worldling answered 13/4, 2014 at 10:46 Comment(3)
What is the external 3rd party library? They might have some #defines you can use to check.Peggie
I can't say what the library is, but you are correct their public header file uses #define, so I'll try thatWorldling
See my related question now, as I give more details: #23103621Worldling
E
1

structs are compile-time artifacts. They tell the compiler how to lay out a region of memory. Once that is done, structs 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);
}

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.

Epstein answered 13/4, 2014 at 11:4 Comment(0)
A
0

AFAIK a classical C function has to exist. It is statically bound during the linking process and it is not, like Objective-C mehtods, dynamically bound on runtime.

So when the code compiles AND links without errors or warnings, then you should be fine.

The same for structs.

Alurta answered 13/4, 2014 at 10:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.