CUnit测试生成错误

CUnit测试生成错误,c,cunit,C,Cunit,这是我第一次测试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> #i

这是我第一次测试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();
}
#包括
#包括
#包括“CUnit/Basic.h”
#包括“calculator\u helper.h”
/*
*CUnit测试套件
*/
int init_套件(无效){
返回0;
}
内部清洁套房(无效){
返回0;
}
/*重要部分:*/
无效测试dd(){
int num1=2;
int num2=2;
int结果=相加(num1,num2);
如果(结果==4){
CU_断言(0);
}
}
int main(){
CU_pSuite pSuite=NULL;
/*初始化CUnit测试注册表*/
if(CUE_SUCCESS!=CU_initialize_registry())
返回CU_get_error();
/*向注册表添加一个套件*/
pSuite=CU_添加套件(“新CUNITTEST”、初始套件、清洁套件);
如果(NULL==pSuite){
CU_cleanup_registry();
返回CU_get_error();
}
/*将测试添加到套件中*/
如果((NULL==CU_添加_测试(pSuite,“testAdd”,testAdd))){
CU_cleanup_registry();
返回CU_get_error();
}
/*使用CUnit基本界面运行所有测试*/
CU_基本设置模式(CU_BRM_详细);
CU_基本_运行_测试();
CU_cleanup_registry();
返回CU_get_error();
}
问题

当我构建测试时,我得到一个构建测试失败的消息。更具体地说,我知道:

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 在函数“add”中:NetBeans/Calculator/Calculator\u helper.h:12: “添加”的多重定义 build/Debug/GNU-Linux-x86/tests/tests/newcunittest.o:NetBeans/Calculator//Calculator\u helper.h:12: 首先在此处定义collect2:错误:ld返回1退出状态 有人能告诉我为什么我会犯这个错误吗。我试着在谷歌上搜索,但没有找到运气。

这:

#ifndef CALCULATOR_HELPER_H
#define CALCULATOR_HELPER_H
#endif
是一个“包含守卫”。但这样做是错误的:你的代码应该在#endif之前,而不是之后

额外提示:不要在代码中使用“helper”这个词——总会有更好的。与本例类似,您可以将其称为
计算器\u数学\u H

如下:

#ifndef CALCULATOR_HELPER_H
#define CALCULATOR_HELPER_H
#endif
是一个“包含守卫”。但这样做是错误的:你的代码应该在#endif之前,而不是之后

额外提示:不要在代码中使用“helper”这个词——总会有更好的。就像在本例中,您可以将其称为
计算器\u数学\u H

我有一个要测试的头文件:

#ifndef CALCULATOR_HELPER_H
#define CALCULATOR_HELPER_H
#endif

    int add(int num1, int num2) {
        return num1 + num2;
    }
您正在头文件中定义函数:

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;
}
建议阅读:

  • 维基百科:
  • 维基百科:
我有一个要测试的头文件:

#ifndef CALCULATOR_HELPER_H
#define CALCULATOR_HELPER_H
#endif

    int add(int num1, int num2) {
        return num1 + num2;
    }
您正在头文件中定义函数:

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;
}
建议阅读:

  • 维基百科:
  • 维基百科:

链接器告诉您“添加”有两种定义。在一分钟内忽略其他回复中提出的有效点,您的代码构建得很好,就像在Ubuntu 12.04.2上使用命令行上的gcc一样。我是这样构建的,没有看到任何警告(已将libcunit.a安装到/usr/local/lib):

然后它运行了,正如您在测试中预期的那样失败:

...
Suite: newcunittest
  Test: testAdd ...FAILED
    1. testsuite.c:25  - 0
...
在这种情况下,您的问题似乎是由于Netbeans中的某些东西也定义了一个“add”函数引起的,或者您的构建比您发布的内容更多,其他文件包括“calculator\u helper.h”,这将导致您的函数由于其损坏的包含保护而被包含和定义两次

您可能还希望更改测试的样式,以便它断言它期望为真的内容。当add()做了正确的事情时,您当前的测试没有通过断言,这不是大多数人所期望的!请尝试以下方法:

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

链接器告诉您“添加”有两种定义。在一分钟内忽略其他回复中提出的有效点,您的代码构建得很好,就像在Ubuntu 12.04.2上使用命令行上的gcc一样。我是这样构建的,没有看到任何警告(已将libcunit.a安装到/usr/local/lib):

然后它运行了,正如您在测试中预期的那样失败:

...
Suite: newcunittest
  Test: testAdd ...FAILED
    1. testsuite.c:25  - 0
...
在这种情况下,您的问题似乎是由于Netbeans中的某些东西也定义了一个“add”函数引起的,或者您的构建比您发布的内容更多,其他文件包括“calculator\u helper.h”,这将导致您的函数由于其损坏的包含保护而被包含和定义两次

您可能还希望更改测试的样式,以便它断言它期望为真的内容。当add()做了正确的事情时,您当前的测试没有通过断言,这不是大多数人所期望的!请尝试以下方法:

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

我将#endif保护放在代码的底部。它仍然不起作用=sOK,下一步,在
intadd
之前添加
inline
,因为在头文件中定义(而不是像更典型的那样,仅声明)函数时需要它。或者你可以按照这里另一个答案的建议,在.cpp文件中定义函数。它仍然不起作用=sOK,下一步,在
intadd
之前添加
inline
,因为在头文件中定义(而不是像更典型的那样,仅声明)函数时需要它。或者你可以按照这里的另一个答案来做,也就是在一个.cpp文件中定义函数。谢谢你的回复。我给出的代码是完整的,这意味着我没有遗漏任何内容。出于某种原因,当我将代码与头文件分离并放入源文件时,它就开始工作了。我的头文件中只有原型。我认为这也与NetBeans有关。如果你想把它固定到NetBeans上,那么就回到原来的代码来恢复问题,并重命名add函数(例如,改为“goaler444_add”)。如果与NetBeans发生冲突,则错误将消失