它是我第一次測試C程序。我有這個頭文件,我想測試:CUnit測試得到構建錯誤
#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試驗。更具體地說,我得到這個:
In function `add': NetBeans/Calculator/calculator_helper.h:12: multiple definition of `add' build/Debug/GNU-Linux-x86/tests/tests/newcunittest.o:NetBeans/Calculator/./calculator_helper.h:12: first defined here collect2: error: ld returned 1 exit status
有人可以告訴我爲什麼我得到這個錯誤。我試圖在谷歌搜索,但我沒有發現運氣。
謝謝。它的工作現在:) – Goaler444 2013-03-02 12:32:54