Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cocoa check if function exists

I'd like to use a function that's only available on OS X 10.9, but WITHOUT compiling with the 10.9 SDK. Is that possible?

I've tried weak linking, but the compiler just gives out an error that the function is not defined.

like image 492
bogdansrc Avatar asked Nov 21 '25 10:11

bogdansrc


1 Answers

You say you don't want to compile against 10.9, but give no reason. Just in case you can:

If you set your target to 10.9 and your deployment to something lower then Xcode will weak link the 10.9 frameworks. You can then test for a C function being available by comparing its name to NULL. This fragment is taken from this document:

extern int MyWeakLinkedFunction() __attribute__((weak_import));

int main()
{
   int result = 0;

   if (MyWeakLinkedFunction != NULL)
   {
      result = MyWeakLinkedFunction();
   }

   return result;
}

(BTW: no sandbox issues this way.)

like image 137
CRD Avatar answered Nov 23 '25 00:11

CRD