0

我正在尝试使用FFF (Fake Function Framework)模拟来自外部库的函数。这些函数的声明有点奇怪,我不知道该怎么做。

库文件

...
// XWF_GetItemInformation
typedef INT64 (__stdcall *fptr_XWF_GetItemInformation) (LONG nItemID, 
   LONG nInfoType, LPBOOL lpSuccess); 
...
extern fptr_XWF_GetItemInformation XWF_GetItemInformation;
...

库文件

...
fptr_XWF_GetItemInformation XWF_GetItemInformation;
...
LONG __stdcall XT_RetrieveFunctionPointers()
{
    ...
    XWF_GetItemInformation = (fptr_XWF_GetItemInformation) GetProcAddress(Hdl, "XWF_GetItemInformation");
    ...
}

我在嘲笑它:

#include <fff/fff.h>
#include <lib.h>

FAKE_VALUE_FUNC(INT64, XWF_GetItemInformation, LONG, LONG, LPBOOL);

但这给出了错误(缩短):

In file included from external/xwf/item.test.cpp:5:
external/fff/fff.h:1793:100: error: 'INT64 XWF_GetItemInformation(LONG, LONG, LPBOOL)' redeclared as different kind of entity
 1793 |     RETURN_TYPE FFF_GCC_FUNCTION_ATTRIBUTES FUNCNAME(ARG0_TYPE arg0, ARG1_TYPE arg1, ARG2_TYPE arg2); \
      |                                                                                                    ^
...
external/xwf/x-tension.fff.h:9:1: note: in expansion of macro 'FAKE_VALUE_FUNC'
    9 | FAKE_VALUE_FUNC(INT64, XWF_GetItemInformation, LONG, LONG, LPBOOL)
      | ^~~~~~~~~~~~~~~
In file included from external/xwf/x-tension.h:14,
                 from external/xwf/x-tension.fff.h:5,
                 from external/xwf/item.test.cpp:7:
external/xwf/x-tension/X-Tension.h:322:36: note: previous declaration 'INT64 (* XWF_GetItemInformation)(LONG, LONG, LPBOOL)'
  322 | extern fptr_XWF_GetItemInformation XWF_GetItemInformation;
      |                                    ^~~~~~~~~~~~~~~~~~~~~~
In file included from external/xwf/item.test.cpp:5:
external/fff/fff.h:1797:100: error: 'INT64 XWF_GetItemInformation(LONG, LONG, LPBOOL)' redeclared as different kind of entity
 1797 |     RETURN_TYPE FFF_GCC_FUNCTION_ATTRIBUTES FUNCNAME(ARG0_TYPE arg0, ARG1_TYPE arg1, ARG2_TYPE arg2){ \
      |
...
In file included from external/xwf/x-tension.h:14,
                 from external/xwf/x-tension.fff.h:5,
                 from external/xwf/item.test.cpp:7:
external/xwf/x-tension/X-Tension.h:322:36: note: previous declaration 'INT64 (* XWF_GetItemInformation)(LONG, LONG, LPBOOL)'
  322 | extern fptr_XWF_GetItemInformation XWF_GetItemInformation;
      |                                    ^~~~~~~~~~~~~~~~~~~~~~
make: *** [Makefile:201: build/xwf/item.test.o] Error 1

有人可以帮我如何模拟这个功能吗?

4

1 回答 1

0

您的编译器输出中提到了该问题:

错误:“INT64 XWF_GetItemInformation(LONG, LONG, LPBOOL)”被重新声明为不同类型的实体

基本上,您需要避免在 lib.cpp 中定义函数。您可以通过以下几种方式做到这一点:

  1. 不要在单元测试项目中包含文件 lib.cpp。仅包含您要模拟的接口的 lib.h 文件。如果您不打算从单元测试项目中调用 lib.cpp 中函数的原始实现并且没有其他依赖项调用这些函数,则此解决方案很好。
  2. 将整个生产源代码或相关模块构建到库文件中,并将其包含在您的单元测试项目中,而不是包含在您的 CPP 和 C 源代码文件中。如果您为该库中的函数创建模拟函数,则该模拟将获得首选项,并且您不会出现重新定义错误。
  3. weak在 lib.cpp 中的函数前添加属性。这将优先于函数的其他定义(单元测试项目中的模拟函数)。
  4. 您可以使用Function Pointer SubstitutionLink Time Substitution。您可以在此处阅读有关这两个选项的更多信息:https ://stackoverflow.com/a/65814339/4441211 (该链接中专门解决了使用 FFF 的函数指针替换)
于 2021-01-26T10:39:27.990 回答