3

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!

Community
  • 1
  • 1
Richard
  • 3,316
  • 30
  • 41
  • possible duplicate of [Weak linking on iPhone refuses to work](http://stackoverflow.com/questions/3002833/weak-linking-on-iphone-refuses-to-work) – Jonathan Grynspan Dec 27 '10 at 20:55
  • @Jonathan Grynspan I re-read [your answer to that question](http://stackoverflow.com/questions/3002833/weak-linking-on-iphone-refuses-to-work/3002845#3002845) and re-tried taking the address of the function I want, but as I said in the Q, that only works if I set compiler optimization to None. – Richard Dec 27 '10 at 21:15
  • And you tried with the ampersand? Which compiler are you using, GCC or LLVM? – Jonathan Grynspan Dec 28 '10 at 19:47
  • I tried all 3 compilers: gcc, llvm, and llvm-gcc. That method finally worked (with compiler optimization on) when I made the function pointer `volatile` as noted in my answer below. – Richard Dec 28 '10 at 20:27
  • @Richard, please do not roll back edits that clean up burninated tags. [availability] was removed for a reason. – Charles Nov 19 '13 at 04:02
  • @Charles You're saying that the tag no longer exists on SO? That should have been mentioned in the edit notes. Also, please provide an alternative. – Richard Nov 19 '13 at 13:27
  • @Richard There is no alternative, it was a [horrible little meta tag](http://meta.stackexchange.com/q/195902/135887). The tag edit interface doesn't have a space to explain why tags are edited. – Charles Nov 19 '13 at 16:12
  • @Charles There's a separate interface for editing only tags? The interface I see for editing (tags and content) includes a line to describe the changes. – Richard Nov 19 '13 at 19:08
  • @Richard Correct. It's [unlocked at 2,000 reputation points with the rest of the no-peer-review-needed edit privs](http://stackoverflow.com/help/privileges). – Charles Nov 19 '13 at 22:02

2 Answers2

2

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

ughoavgfhw
  • 39,734
  • 6
  • 101
  • 123
1

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.

Community
  • 1
  • 1
Richard
  • 3,316
  • 30
  • 41