cgcclinkerundefined-referencecmocka

"undefined reference to `_cmocka_run_group_tests'" when running sample CMocka test


I installed the CMocka testing framework and tried the sample code:

#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>

/* A test case that does nothing and succeeds. */
static void null_test_success(void **state) {
    (void) state; /* unused */
}
int main(void) {
    const struct CMUnitTest tests[] = {
            cmocka_unit_test(null_test_success),
    };
    return cmocka_run_group_tests(tests, NULL, NULL);
}

But when I try to compile I get the following error:

$ gcc -o Tests tests.c
    /tmp/ccbwAXrr.o: In function `main':
    tests.c:(.text+0x5e): undefined reference to `_cmocka_run_group_tests'
    collect2: error: ld returned 1 exit status

What am I missing?


Solution

  • Including the header files provides the forward declaration of the functions. To get the function definitions, you need to link with the library.

    You can use -l option with gcc to link to the required libarary. You may also need to use -L option to provide the path to the library.