运行时 API 可用性检查(弱链接)——10.5 上的不正确行为
我正在 10.6 上构建应用程序,但部署目标是 10.5。当程序在 10.6 上运行时,我想利用服务管理 SMJobBless api,但在 10.5 上运行时,我显然仍然需要使用特权安装程序工具。
我与可执行目标中的服务管理框架的链接很弱。我尝试了代码的几种变体:
if (SMJobBless != NULL) ...
if (SMJobBless) ...
bool const /* or non-const */ useBlessAPI = SMJobBless != NULL;
if (useBlessAPI) ...
我什至尝试使用 类似问题中列出的编译器标志。
在 10.6 上,printf("%p %d", SMJobBless, SMJobBless != NULL)
(正确地)为 SMJobBless 打印非空指针值,为非空打印 1。
当我将应用程序包复制到 10.5 时,printf
告诉我 SMJobBless 是 0x0,但(错误地)为非空指针打印 1。
我让它工作的唯一方法是关闭所有优化并将函数指针分配给变量。
Boolean (* const blessAPI) (CFStringRef, CFStringRef, AuthorizationRef, CFErrorRef *) = &SMJobBless;
但我无法关闭生产代码的优化!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试将 extern Boolean SMJobBless() __attribute__((weak_import)); 放入使用该函数的文件中。它可能没有被正确地标记为弱。
Try putting
extern Boolean SMJobBless() __attribute__((weak_import));
in your files that use the function. It might not be getting marked as weak properly.我还发现,类似于评论中引用的问题/答案,如果我将函数指针分配给一个
易失性
变量,那么一切都会评估正常。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.