如何便携式弱链接?弱的#pragma my_symbol [英] How portable is weak linking? #pragma weak my_symbol

查看:270
本文介绍了如何便携式弱链接?弱的#pragma my_symbol的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何便携式弱链接?

 的#pragma弱my_symbol

我看这个问题:<一href=\"http://stackoverflow.com/questions/274753/how-to-make-weak-linking-work-with-gcc\">how-to-make-weak-linking-work-with-gcc讨论如何得到它的工作。但有要做到这一点,这样的gcc没有要求的好办法?

什么是弱连接,并与#ifdef来守卫declartion?

之间的区别

 的#ifndef my_weak_fn
    无效my_weak_fn(){/ *什么也不做* /回报;}
#万一


解决方案

的#pragma ,顾名思义,的的便携性。

和弱链接在链接时(足够令人惊讶)来完成。它允许具有相同签名重写另一个函数(或任何符号,真的)。这意味着一个强大的人会在preference选择弱者之一,但,如果没有强大的之一,弱者将被使用。

包含卫士是由编译器,而不是连接器完成的,他们做的的允许回退到一个很脆弱的,如果强一个人的不存在。如果你控制了这两个函数(源通过 IFDEF -ing一个你可以模拟相同的行为, IFNDEF - ING为其他),但是这并不总是如此,这是两强之间的功能选择更多。

弱链接可以让你不喜欢的东西在自己的的malloc()降)/免费(为了调试的目的,同时还与他们提供的库链接。

How portable is weak linking?

#pragma weak my_symbol

I see that question: how-to-make-weak-linking-work-with-gcc discusses how to get it working. But is there a good way to do this such that gcc is not required?

What is the difference between weak linking and guarding the declartion with an #ifdef?

#ifndef my_weak_fn
    void my_weak_fn(){/* Do nothing */ return;}
#endif

解决方案

#pragma is, by definition, not portable.

And weak linking is done at link time (surprisingly enough). It allows a function (or any symbol, really) with the same signature to override another. That means a strong one will be chosen in preference to the weak one but, if there's no strong one, the weak one will be used.

Include guards are done by the compiler, not the linker, and they do not allow the fallback to a weak one if the strong one's not there. You can simulate the same behavior if you control the source for both functions (by ifdef-ing for one, ifndef-ing for the other) but that's not always the case and this is more choosing between two strong functions.

Weak linking allows you to do things like drop in your own malloc()/free() for debugging purposes while still linking with the library that provides them.

这篇关于如何便携式弱链接?弱的#pragma my_symbol的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆