"undefined reference to `_cmocka_run_group_tests'" 当 运行 样本 CMocka 测试

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

我安装了 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);
}

但是当我尝试编译时出现以下错误:

$ 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

我错过了什么?

包含头文件提供了函数的前向声明。要获取函数定义,您需要 link 库。

您可以使用 -l 选项和 gcc 到 link 到所需的库。您可能还需要使用 -L 选项来提供库的路径。