Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 673
Richard Avatar asked Feb 16 '26 13:02

Richard


1 Answers

Try putting extern Boolean SMJobBless() __attribute__((weak_import)); in your files that use the function. It might not be getting marked as weak properly.

like image 144
ughoavgfhw Avatar answered Feb 18 '26 04:02

ughoavgfhw



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!