有没有办法重新定义在链接时的malloc在Windows? [英] Is there a way to redefine malloc at link time on Windows?

查看:100
本文介绍了有没有办法重新定义在链接时的malloc在Windows?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在链接时替换默认的malloc使用自定义的malloc。但是,当我尝试在我的程序重新定义的malloc,我得到这个错误:

I would like to replace the default malloc at link time to use a custom malloc. But when I try to redefine malloc in my program, I get this error:

MSVCRT.lib(MSVCR80.dll) : error LNK2005: _malloc already defined in test.lib(test.obj)

这完美的作品在任何Unix,它适用于Windows与大多数功能,但不能使用malloc。我怎样才能做到这一点?什么是禁止使用覆盖它的malloc有什么不同?

This works perfectly on any Unix, and it works on Windows with most functions, but not with malloc. How can I do this? And what is different with malloc that disallow overriding it?

我知道我可以代替每次调用我的自定义的malloc为malloc或使用宏来做到这一点,但我宁愿不要修改每一个第三方库。

I know I could replace every call to malloc with my custom malloc, or use a macro to do this, but I would rather not modify every third party library.

推荐答案

有是的,这是怎么在这里实在难以很好的讨论:

There is really good discussion of how hard this is here:

<一个href=\"http://benjamin.smedbergs.us/blog/2008-01-10/patching-the-windows-crt/\">http://benjamin.smedbergs.us/blog/2008-01-10/patching-the-windows-crt/

显然,你需要修补CRT

编辑:实际上,一个MS员工给了讨论的技术。您需要将您的malloc移动到一个lib,然后在CRT

actually, a MS employee gave the technique in the discussion. You need to move your malloc to a lib, and then link it before the CRT

他还提到,如果你链接您的malloc作为一个lib的的在CRT(即确保打开忽略默认库和显式包括CRT),你会得到什么你想要的,可以重新分配这个lib中没有任何问题。

"he also mentions that if you link your malloc as a lib before the CRT (i.e. make sure to turn on ‘ignore default libs’ and explictly include the CRT), you’ll get what you want, and can redistribute this lib without problems."

这篇关于有没有办法重新定义在链接时的malloc在Windows?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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