问题
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