运行时 API 可用性检查(弱链接)——10.5 上的不正确行为

发布于 2024-10-09 08:11:42 字数 840 浏览 0 评论 0原文

我正在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寂寞陪衬 2024-10-16 08:11:42

尝试将 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.

羁绊已千年 2024-10-16 08:11:42

我还发现,类似于评论中引用的问题/答案,如果我将函数指针分配给一个易失性变量,那么一切都会评估正常。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文