macosapicompatibilityweak-linking

Runtime API availability check (weak linking) -- incorrect behavior on 10.5


I'm building my application on 10.6 but targeting 10.5 for deployment. I want to take advantage of the Service Management SMJobBless api when the program will run on 10.6, but I obviously will still need to use a privileged installer tool when running on 10.5.

I weakly link to the Service Management framework in my executable target. I have tried several variations of the code:

if (SMJobBless != NULL) ...

if (SMJobBless) ...

bool const /* or non-const */ useBlessAPI = SMJobBless != NULL;
if (useBlessAPI) ...

And I've even tried using the compiler flags listed in a similar-seeming question.

On 10.6, printf("%p %d", SMJobBless, SMJobBless != NULL) (correctly) prints a non-null pointer value for SMJobBless and 1 for non-null.

When I copy the app bundle to 10.5, the printf tells me that SMJobBless is 0x0, but (incorrectly) prints 1 for a non-null pointer.

The only way I've gotten it to work is by turning off all optimizations and assigning the function pointer to a variable.

Boolean (* const blessAPI) (CFStringRef, CFStringRef, AuthorizationRef, CFErrorRef *) = &SMJobBless;

But I can't turn off optimization for production code!


Solution

  • I also found that, similar to the question/answer cited in comments, if I assigned the function pointer to a volatile variable, then everything evaluated ok.