4

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?

Sourav Ghosh
  • 127,934
  • 16
  • 167
  • 234
neo post modern
  • 1,164
  • 10
  • 27

1 Answers1

5

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.

Sourav Ghosh
  • 127,934
  • 16
  • 167
  • 234
  • 1
    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](http://stackoverflow.com/questions/480764/linux-error-while-loading-shared-libraries-cannot-open-shared-object-file-no-s#comment7282227_480764). – neo post modern Apr 29 '15 at 14:00
  • 1
    @neopostmodern that's why i used __may__ :-) – Sourav Ghosh Apr 29 '15 at 14:01
  • @SouravGhosh - [Why is there a 15 minute time limit on accepting an answer in StackOverflow?](http://meta.stackexchange.com/questions/50697/time-limit-on-accepting-an-answer) – neo post modern Apr 29 '15 at 14:14
  • 1
    I have exactly the same problem, but this solutions doesnt help – Gill Bates Aug 24 '15 at 17:56
  • The source of my problem was that I called `gcc -lcmocka cmockatest.c` instead of `gcc cmockatest.c -lcmocka`. – Gill Bates Aug 25 '15 at 11:56