覆盖静态库中定义的 C 函数 [英] Override a C function defined in a static library

查看:35
本文介绍了覆盖静态库中定义的 C 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 C 文件的静态库,在 Cygwin 上用 g++ 编译.我希望对库中定义的一个函数进行单元测试.该函数调用该库中定义的另一个函数,我希望覆盖依赖项以用我自己的该函数版本替换它.我无法修改静态库中的内容,所以这个解决方案 [ Override a function call inC ] 不适用.

I have a static library of C files, compiled with g++ on Cygwin. I wish to unit test one function that is defined in the library. That function calls another function defined in that library and I wish to override the dependency to replace it with my own version of that function. I can't modify what's in the static library, so this solution [ Override a function call in C ] doesn't apply.

通常,我可以编写一个 .cpp 文件并包含包含我要进行单元测试的函数的 .c 文件,该文件实质上是使用我添加的代码扩展该文件.这是一个我永远不会用于生产代码的肮脏技巧,但它对于单元测试 C 文件很方便,因为它使我的测试代码可以访问该 C 文件中的静态内容.然后,我可以编写我的依赖项的假版本,以及调用我正在测试的函数的单元测试函数.我编译 my.cpp 以获取 my.o,然后将其与静态库链接.理论上,由于链接器已经找到了依赖项的定义(我提供的那个),它不会在库中查找并且不会发生冲突.通常这是可行的,但现在我得到一个多重定义"错误,链接器首先找到我的假货,然后找到真正的.我不知道是什么原因造成的,也不知道要寻找什么.我也不能把它归结为一个简单的例子,因为我的简单例子没有这个问题.

Usually, I can write a .cpp file and include the .c file containing the function I want to unit test, which essentially extends that file with the code I add. It's a dirty trick I'd never use for production code but it's handy for unit testing C files, because it gives my test code access to static things in that C file. Then, I can write in my fake version of the dependency, and my unit test function that calls the function I'm testing. I compile my.cpp to get my.o, then link it with the static library. In theory, since the linker has found a definition for the dependency already (the one I provide) it won't look in the library and there will be no conflict. Usually this works, but now I'm getting a "multiple definition" error where the linker first finds my fake and then finds the real one. I don't know what might cause this and don't know what to look for. I also can't boil this down to a simple example because my simple examples don't have this problem.

请给点意见?

推荐答案

一种可能性(诚然,也很丑陋,但是......)是从静态库中提取单个目标文件.如果您正在调用的函数和 it's 调用的函数位于不同的目标文件中,您可以链接到包含您需要调用的函数的目标文件,但不能链接到包含它调用的函数的目标文件.

One possibility (admittedly, and ugly one, but...) is to extract the individual object files from the static library. If the function you're calling and the function it's calling are in separate object files, you can link against the object file containing the function you need to call, but not against the one containing the function it calls.

不过,这只会为您提供完整目标文件级别的粒度,因此如果涉及的两个函数都在同一个目标文件中,它将无法工作.如果您真的需要让事情正常工作,并且不介意对相关目标文件进行真正微小的修改,您可以能够使用二进制编辑器将第二个函数标记为弱外部,这意味着它将在没有任何其他同名外部的情况下使用,但如果提供了另一个,则会使用它.

This only gives you granularity on the level of complete object files though, so if the two functions involved are both in the same object file, it won't work. If you really need to get things to work, and don't mind making a really minor modification to the object file in question, you may be able to use a binary editor to mark the second function as a weak external, which means it'll be used in the absence of any other external with the same name, but if another is provided, that will be used instead.

后者是否符合修改库"的条件在一定程度上取决于您的观点.它没有修改库中的 code,而是修改了一些围绕该代码的目标文件包装器.我的猜测是你宁愿不这样做,但它可能仍然是摆脱原本站不住脚的最干净的方法.

Whether that latter qualifies as "modifying the library" or not depends a bit on your viewpoint. It's not modifying the code in the library, but is modifying a bit of the object file wrapper around that code. My guess is that you'd rather not do it, but it may still be the cleanest way out of an otherwise untenable situation.

这篇关于覆盖静态库中定义的 C 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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