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?

Community
  • 1
  • 1
Nathan H
  • 44,105
  • 54
  • 154
  • 235
  • What is the external 3rd party library? They might have some `#define`s you can use to check. – Rich Apr 13 '14 at 10:48
  • I can't say what the library is, but you are correct their public header file uses #define, so I'll try that – Nathan H Apr 13 '14 at 11:17
  • See my related question now, as I give more details: http://stackoverflow.com/questions/23103121/detect-and-use-optional-external-c-library-at-runtime-in-objective-c – Nathan H Apr 16 '14 at 07:55

2 Answers2

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.

Sergey Kalinichenko
  • 675,664
  • 71
  • 998
  • 1,399
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.

Hermann Klecker
  • 13,792
  • 4
  • 45
  • 69