当我用 C++ 编写单元测试时,通常需要创建一些自由函数的 stub 方法定义,这些函数在头文件 actions.h 中声明:
#ifdef __cplusplus
extern "C" {
#endif
void action1(void);
void action2(void);
int action3(void);
#ifdef __cplusplus
}
#endif
而不是在单独的文件中创建 stub
void action1() {}
void action2() {}
int action3() { return 0; }
当包含此 header 时,在测试代码中动态定义 stub 会很方便:
create_stubs(#include <actions.h>)
最佳答案
Is it possible to use the preprocessor to create stubs from a header file?
不,这是不可能的。
使用另一种语言解析 header 并生成源文件。 Python、Perl、AWK 或类似语言。
关于c++ - 是否可以使用预处理器或编译器从头文件创建 stub ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70958245/