有什么方法来替换库中的函数吗? [英] Is there any way to replace a function in a library?

查看:331
本文介绍了有什么方法来替换库中的函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用一个库来定义一个脚本语言的内部除法运算符。不幸的是,它不会对除数进行零检查。这导致很多头痛。我知道运算符的签名。



double ScriptClass :: Divide(double& amp; double&); / p>

很遗憾,它甚至不是一个C函数。有没有办法让我的应用程序使用我自己的 Divide 函数而不是 ScriptClass :: Divide function? p>

编辑:
我知道 dlopen(NULL,..)用户定义的。对于类成员函数可以这样做(不诉诸使用复制名称)?

解决方案

一般来说,而不是基本的除法运算符来防止除以零。如果你除以零,很多,似乎表明在使用的算法可能的缺陷。考虑重做算法,或者如果这不是一个选项,保护调用以零检查划分。你甚至可以在 protected_divide 类型函数内部执行此操作。



一个C ++函数你有一个C ++库编译所有相同的选项,你使用它来构建你的应用程序,所以名称mangling匹配你可能能够重新定义该函数为 .so 并使用 LD_PRELOAD 强制加载它。如果你静态链接,我会你可以创建该函数到你自己的 .o 文件,并链接之前的库本身将导致链接器以取得您的版本。


I work with a library which defines its internal division operator for a scripting language. Unfortunately it does not zero-check the divisor. Which leads to lot of headaches. I know the signature of the operator.

double ScriptClass::Divide(double&, double&);

Sadly it isn't even a C function. Is there any way I could make my application use my own Divide function instead of ScriptClass::Divide function?

EDIT: I was aware of dlopen(NULL,..) and replacing "C" functions with user defined ones. Can this be done for class member functions (Without resorting to using mangled names)?

解决方案

Generally speaking it's up to the programmer, not the underlying divide operator to prevent division by zero. If you're dividing by zero a lot that seems to indicate a possible flaw in the algorithm being used. Consider reworking the algorithm, or if that's not an option, guard calls to divide with a zero check. You could even do that inside a protected_divide type function.

All that being said, assuming that since it looks like a C++ function you have a C++ library compiled with all the same options you're using to build your application so name mangling matches you might be able to redefine the function into a .so and use LD_PRELOAD to force it to load. If you link statically, I think you can create the function into your own .o file and linking that prior to the library itself will cause the linker to pick up your version.

这篇关于有什么方法来替换库中的函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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