2013-03-01 36 views
2

它是我第一次測試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 

有人可以告訴我爲什麼我得到這個錯誤。我試圖在谷歌搜索,但我沒有發現運氣。

回答

2

我有這個頭文件,我想測試:你定義

在頭文件中的函數:

int add(int num1, int num2) { 
    return num1 + num2; 
} 

聲明它在標題:

#ifndef CALCULATOR_HELPER_H 
#define CALCULATOR_HELPER_H 

int add(int num1, int num2); 

#endif  /* the endif goes at the end of the file */ 

......和在源文件中定義它

#include "helper.h" 

int add(int num1, int num2) { 
    return num1 + num2; 
} 

推薦閱讀:

+0

謝謝。它的工作現在:) – Goaler444 2013-03-02 12:32:54

1

此:

#ifndef CALCULATOR_HELPER_H 
#define CALCULATOR_HELPER_H 
#endif 

是一個 「包括衛士」。但它做錯了:你的代碼應該在#endif之前,而不是之後。

獎勵提示:不要在代碼中使用「助手」一詞 - 總會有更好的一個。就像在這種情況下,你可以稱它爲CALCULATOR_MATH_H

+0

我把#endif守衛放在我的代碼底部。它仍然沒有工作= s – Goaler444 2013-03-01 17:18:23

+0

好的,下一步,在int add之前添加'inline',因爲在頭文件中定義函數(而不是像聲明那樣更典型)時需要這樣做。或者你可以在這裏做另一個答案,這就是在.cpp文件中定義函數。 – 2013-03-02 02:07:51

1

的鏈接器告訴你你有兩個「添加」的定義。忽略其他答覆提出的有效問題一分鐘後,您的代碼會在Ubuntu 12.04.2上使用命令行上的gcc進行良好構建。我像這樣構建它,並沒有看到警告(已安裝libcunit。一到/ usr/local/lib目錄):

gcc -Wall -c testsuite.c 
gcc testsuite.o -L/usr/local/lib -lcunit -static -o testsuite 

,它跑了,沒有你可能期望給你的測試:

... 
Suite: newcunittest 
    Test: testAdd ...FAILED 
    1. testsuite.c:25 - 0 
... 

這是似乎是你的問題或者通過引起的情況下Netbeans中的一些內容也定義了一個「add」函數,或者你的內部版本比你發佈的還要多,其他文件包括「calculator_helper.h」,這會導致你的函數被包含並定義兩次,這要歸功於它的破壞包括守衛。

您可能還想更改測試樣式,以便聲明其預期爲真的樣式。你的當前測試在add()做正確的事情時失敗了,這不是大多數人所期望的!試試這個:

void testAdd() { 
    int num1 = 2; 
    int num2 = 2; 
    int result = add(num1, num2); 
    CU_ASSERT(result == 4); 
} 
+0

感謝您的回覆。我提交的代碼是完整的,這意味着我沒有留下任何東西。出於某種原因,當我將代碼從頭文件中分離出來並放入源文件時,它開始工作。我所有的頭文件都是原型。我認爲它也與NetBeans有關。 – Goaler444 2013-03-15 16:37:13

+0

如果您想將其指定到NetBeans,請恢復爲原始代碼以恢復問題並重命名您的添加功能(例如,添加到「goaler444_add」)。如果它與NetBeans發生衝突,那麼錯誤將消失。 – 2013-03-15 17:07:35

相關問題