关于CPPUTEST的那点事

本文介绍如何在CPPUTEST项目中模拟系统API或已存在的函数。首先,通过设置编译参数避免多重定义的问题。其次,确保模拟文件比源代码先被编译。最后,演示了如何在模拟文件中重新定义源文件中的函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. How to stub the system API or existed functions in CPPUTEST project?

A. Firstly, you need to add "-Wl,--allow-multiple-definition" to CPPUTEST_CPPFLAGS to avoid multiple-definition's problem.

CPPUTEST_CPPFLAGS += -Wl,--allow-multiple-definition
Note: this parameter involved safe problem , it just use for stub.c file...

B. Secondly, compiling stub.c should earlier than src file.

SRC_DIRS = \
stubs\
src
You can try to stub the same name of functions in stub.c now.

Source file: src/hello.c

#include "hello.h"

int exe_cmd(const char* cmd)
{
....................
return system(cmd);
}

Stub file: stub/stub.c

#include "CppUTestExt/MockSupport_c.h"

int exe_cmd(const char* cmd)
{
do your expectation.....
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值