dangeroushobo dangeroushobo - 2 months ago 51
C Question

libcheck test fails to link

I'm trying to build a simple example using libcheck but it fails to build while using the flags reported by pkg-config.

My file: tests/test.c

#include <stdlib.h>
#include <check.h>

START_TEST(zero)
{
int z = 0;
ck_assert_int_eq(0, z);
}
END_TEST

Suite* suite_zero(void)
{
Suite* s;
TCase* tc;
s = suite_create("suite_zero");
tc = tcase_create("zero");
tcase_add_test(tc, zero);

suite_add_tcase(s, tc);
return s;
}

int main(void)
{
int number_failed;
SRunner* sr;

Suite* s_zero = suite_zero();

sr = srunner_create(s_zero);

srunner_run_all(sr, CK_NORMAL);
number_failed = srunner_ntests_failed(sr);
srunner_free(sr);
return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}


My system:

$ cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=16.04
DISTRIB_CODENAME=xenial
DISTRIB_DESCRIPTION="Ubuntu 16.04 LTS"
$ gcc --version
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.2) 5.4.0 20160609

$ pkg-config --version
0.29.1


When I try to build my test using the flags and libs reported by pkg-config:

$ pkg-config --cflags --libs check
-pthread -lcheck_pic -pthread -lrt -lm -lsubunit
$ gcc -pthread -lcheck_pic -pthread -lrt -lm -lsubunit -g tests/test.c -o tests/zero
/tmp/ccRV2kLw.o: In function `zero':
Code/tests/test.c:4: undefined reference to `tcase_fn_start'
/Code/tests/test.c:7: undefined reference to `_mark_point'
/Code/tests/test.c:7: undefined reference to `_ck_assert_failed'
/tmp/ccRV2kLw.o: In function `suite_zero':
/Code/tests/test.c:15: undefined reference to `suite_create'
/Code/tests/test.c:16: undefined reference to `tcase_create'
/Code/tests/test.c:17: undefined reference to `_tcase_add_test'
/Code/tests/test.c:19: undefined reference to `suite_add_tcase'
/tmp/ccRV2kLw.o: In function `main':
/Code/tests/test.c:30: undefined reference to `srunner_create'
/Code/tests/test.c:32: undefined reference to `srunner_run_all'
/Code/tests/test.c:33: undefined reference to `srunner_ntests_failed'
/Code/tests/test.c:34: undefined reference to `srunner_free'
collect2: error: ld returned 1 exit status


Libcheck was installed by apt and the library and headers are in the typical locations, /usr/lib and /usr/include respectively. Yet it appears it can't be found. I'm a bit stumped as why. Any ideas?

Thanks.

Answer

Try adding the flags reported by pkg-config after the compilation command as this answer and the documentation for pkg-config suggests. The compiler needs to know what file it's trying to link the library to beforehand so that the flags can be applied appropriately.

gcc -g test.c -o test -pthread -lcheck_pic -pthread -lrt -lm -lsubunit

or more succinctly

gcc -g test.c -o test `pkg-config --cflags --libs check`

I would actually appreciate it if someone could provide a better explanation as to why the flags come after gcc, I'm not sure my explanation qualifies. :)