2

Its my first time testing C programs. I have this header file which I would like to test:

#ifndef CALCULATOR_HELPER_H
#define CALCULATOR_HELPER_H
#endif

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

I am using the framework CUnit to test it. I am using Netbeans as an IDE. The following is the code.

#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();
}

PROBLEM

When I am building the test, I am getting a BUILD TESTS FAILED. More specifically, I get this:

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

Can anybody tell me why I am getting this error. I tried searching on google but I found no luck.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
Goaler444
  • 2,591
  • 6
  • 35
  • 53

3 Answers3

2

I have this header file which I would like to test:

You're defining a function in a header file:

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

Declare it in the header:

#ifndef CALCULATOR_HELPER_H
#define CALCULATOR_HELPER_H

int add(int num1, int num2);

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

...and define it in a source file:

#include "helper.h"

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

Recommended reading:

Community
  • 1
  • 1
netcoder
  • 66,435
  • 19
  • 125
  • 142
1

This:

#ifndef CALCULATOR_HELPER_H
#define CALCULATOR_HELPER_H
#endif

Is an "include guard". But it's done wrong: your code is supposed to go before the #endif, not after.

Bonus tip: don't use the word "helper" in code--there's always a better one. Like in this case, you could call it CALCULATOR_MATH_H.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • I placed the #endif guard at the bottom of my code. It still didnt work =s – Goaler444 Mar 01 '13 at 17:18
  • OK, next step, add `inline` before `int add`, because you need that when defining (rather than just declaring, as is more typical) functions in header files. Or you can do what another answer here suggests, which is to define the function in a .cpp file. – John Zwinck Mar 02 '13 at 02:07
1

The linker is telling you that there are two definitions of "add". Ignoring for a minute the valid points raised by the other replies, your code builds fine as it is on Ubuntu 12.04.2 using gcc on the command line. I built it like this and saw no warnings (having installed libcunit.a to /usr/local/lib):

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

and it ran, failing as you might expect given your test:

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

That being the case it seems that your problem is caused either by something in Netbeans also defining an "add" function, or there's more to your build than you posted and other files include "calculator_helper.h", which would result in your function being included and defined twice thanks to its broken include guard.

You might also want to change the style of your test so that it asserts what it expects to be true. Your current test fails an assertion when add() does the right thing, which is not what most people would expect! Try this instead:

void testAdd() {
    int num1 = 2;
    int num2 = 2;
    int result = add(num1, num2);
    CU_ASSERT(result == 4);
}
  • Thanks for your reply. The code I presented was complete, meaning that I didnt leave anything out. For some reason, when I separated the code from the header file and put in source files, it started to work. All I have in my header file are prototypes. I think it has to do with NetBeans too. – Goaler444 Mar 15 '13 at 16:37
  • If you want to nail it down to NetBeans then revert to your original code to reinstate the problem and rename your add function (e.g. to "goaler444_add"). If it's a clash with NetBeans then the error will go away. – Robin Mallinson Mar 15 '13 at 17:07