c中的包装功能 [英] Wrapping function in c
问题描述
我有任务为现有项目(用C编写)编写单元测试(使用cunit和cmocka),并且遇到了以下问题.当我包装在测试文件中定义的函数时,仅原始函数被调用.另外,无法更改测试文件的来源. 我阅读的所有内容都不满足第二个条件,但另一方面,很难相信单元测试框架没有为此类问题做好准备.
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?
我试图通过在cmake文件中添加适当的链接器标志来包装它.
I've tried to wrap it by adding appropriate linker flag to cmake file.
推荐答案
是否可以将函数调用包装到在一个文件中定义和调用的函数?
is it possible to wrap function call to function that is defined and called in one file?
如您所知,
cmocka
利用了链接器的--wrap
选项. --wrap=symbol
的文档告诉我们,答案为
您的问题是:
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 =符号
--wrap=symbol
对符号使用包装函数.对符号的任何未定义引用将是 解析为__wrap_symbol.对__real_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.
对象文件中对symbol
的任何引用也包含
symbol
的定义不是未定义的引用,因此链接器将不是
将该引用解析为__wrap_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.
这篇关于c中的包装功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!