这是我第一次测试 C 程序。我有这个我想测试的头文件:
#ifndef CALCULATOR_HELPER_H
#define CALCULATOR_HELPER_H
#endif
int add(int num1, int num2) {
return num1 + num2;
}
我正在使用框架 CUnit 来测试它。我正在使用 Netbeans 作为 IDE。以下是代码。
#include <stdio.h>
#include <stdlib.h>
#include "CUnit/Basic.h"
#include "calculator_helper.h"
/*
* CUnit Test Suite
*/
int init_suite(void) {
return 0;
}
int clean_suite(void) {
return 0;
}
/* IMPORTANT PART: */
void testAdd() {
int num1 = 2;
int num2 = 2;
int result = add(num1, num2);
if (result == 4) {
CU_ASSERT(0);
}
}
int main() {
CU_pSuite pSuite = NULL;
/* Initialize the CUnit test registry */
if (CUE_SUCCESS != CU_initialize_registry())
return CU_get_error();
/* Add a suite to the registry */
pSuite = CU_add_suite("newcunittest", init_suite, clean_suite);
if (NULL == pSuite) {
CU_cleanup_registry();
return CU_get_error();
}
/* Add the tests to the suite */
if ((NULL == CU_add_test(pSuite, "testAdd", testAdd))) {
CU_cleanup_registry();
return CU_get_error();
}
/* Run all tests using the CUnit Basic interface */
CU_basic_set_mode(CU_BRM_VERBOSE);
CU_basic_run_tests();
CU_cleanup_registry();
return CU_get_error();
}
问题
当我构建测试时,我得到了 BUILD TESTS FAILED。更具体地说,我明白了:
在函数“添加”中:NetBeans/Calculator/calculator_helper.h:12: 'add' 的多重定义 构建/调试/GNU-Linux-x86/tests/tests/newcunittest.o:NetBeans/Calculator/./calculator_helper.h:12: 首先在这里定义 collect2: error: ld returned 1 exit status
谁能告诉我为什么我会收到这个错误。我尝试在谷歌上搜索,但没有找到运气。