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

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

问题描述

我的C文件,相克在Cygwin ++编译静态库。我想单元测试是在库中定义一个函数。这函数调用该库定义的另一个功能,我希望覆盖的依赖性用我自己的版本的功能来取代它。我不能修改什么是静态库,因此该解决方案[覆盖在函数调用ç]不适用。

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文件,其中包括含有单元测试,该部分基本上延伸与code我再补充一点的文件我想要的功能.c文件。这是一个肮脏的把戏我从来没有生产code使用,但它的方便进行单元测试C文件,因为它给我的测试code使用一成不变的东西在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.

想法,请?

推荐答案

其中的可能的(当然,和丑陋的,但是...),是从静态库中提取的目标文件。如果你调用的功能和作用的的呼叫在不同的目标文件,就可以对目标文件链接包含需要调用的函数,而不是针对包含它调用函数之一

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 的在库中,但正在修改了一下周围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天全站免登陆