Wrapping function in c

自古美人都是妖i 提交于 2019-12-11 07:36:25

问题


I got task to write unit tests(using cunit and cmocka) for existing project(written in C), and a met following problem. When I wrap function that is defined in tested file, only original function is called. Additional, a can't change source of tested file. Everything I read didn't met the second condition, but on the other hard it is pretty hard to believe that unit test framework is not prepared for this kind of problem.

So is it possible to wrap function call to function that is defined and called in one file?

I've tried to wrap it by adding appropriate linker flag to cmake file.


回答1:


is it possible to wrap function call to function that is defined and called in one file?

cmocka leverages the linker's --wrap option, as you know. The documentation of --wrap=symbol tells us that the answer to your question is No:

--wrap=symbol

Use a wrapper function for symbol. Any undefined reference to symbol will be resolved to __wrap_symbol. Any undefined reference to __real_symbol will be resolved to symbol.

Any reference to symbol that is in an object file that also contains the definition of symbol is not an undefined reference, so the linker will not resolve that reference to __wrap_symbol. The definition must be compiled into some other object file in the linkage for wrapping of the reference to occur.



来源:https://stackoverflow.com/questions/48664718/wrapping-function-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!