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

1

5

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?

Interlocutrix answered 29/4, 2015 at 13:51 Comment(0)
K
7

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.

Kuo answered 29/4, 2015 at 13:54 Comment(5)
I now successfully ran gcc -o Tests tests.c -l cmocka -L /usr/local/lib but I'm pretty sure the -L is redundant. I had forgotten to do ldconfig too, but thankfully there is always a StackOverflow question.Interlocutrix
@neopostmodern that's why i used may :-)Kuo
@SouravGhosh - Why is there a 15 minute time limit on accepting an answer in StackOverflow?Interlocutrix
I have exactly the same problem, but this solutions doesnt helpGaucherie
The source of my problem was that I called gcc -lcmocka cmockatest.c instead of gcc cmockatest.c -lcmocka.Gaucherie

© 2022 - 2024 — McMap. All rights reserved.