So I am trying to learn how to use the library check with a simple example on MacOS 11.6.1. For this I copied the code of Merlijn Sebrechts of the following question: Using C unit testing framework Check without Autotools?
#include <check.h>
START_TEST (sanity_check)
{
fail_unless(5 == 5, "this should succeed");
fail_unless(6 == 5, "this should fail");
ck_assert_str_eq("asa", "asasdfasdf");
}
END_TEST
int main(void)
{
Suite *s1 = suite_create("Core");
TCase *tc1_1 = tcase_create("Core");
SRunner *sr = srunner_create(s1);
int nf;
suite_add_tcase(s1, tc1_1);
tcase_add_test(tc1_1, sanity_check);
srunner_run_all(sr, CK_ENV);
nf = srunner_ntests_failed(sr);
srunner_free(sr);
return nf == 0 ? 0 : 1;
}
When I execute the command in the terminal, I get the following error message:
$ gcc test.c -Wall -o test -lcheck -pthread -lcheck_pic -pthread -lrt -lm -lsubunit
ld: library not found for -lcheck_pic
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I check that the library check is installed by compiling the file following file (test2.c
) with gcc test2.c
and did not get any error message. From this I assumed that the library is indeed installed
#include <check.h>
int main(){
int a;
return 0;
}
What am I doing wrong?
EDIT 1
Ok following the comment, I removed calling most of the libraries and run the following command:
gcc test.c -Wall -o test -lcheck
$ ./test
Running suite(s): Core
0%: Checks: 1, Failures: 1, Errors: 0
test.c:6:F:Core:sanity_check:0: this should fail
Is it correct? And I have to say frankly: I do not understand what the code is doing?
EDIT 2
After the new comment of Jason, I had to serially removed several libraries in order not get an error by build (namely lcheck_pic
, lsubunit
, lrt
:
$ gcc test.c -Wall -o test -lcheck -pthread -lcheck_pic -pthread -lrt -lm -lsubunit
ld: library not found for -lcheck_pic
$ gcc test.c -Wall -o test -lcheck -pthread -pthread -lm -lsubunit
ld: library not found for -lsubunit
clang: error: linker command failed with exit code 1 (use -v to see invocation)
$ gcc test.c -Wall -o test -lcheck -pthread -pthread -lrt -lm -lsubunit
ld: library not found for -lrt
clang: error: linker command failed with exit code 1 (use -v to see invocation)
-v to see invocation)
$ gcc test.c -Wall -o test -lcheck -pthread -pthread -lm
... build successful ...
EDIT 3
In order to have every failure analyzed individually, I separated the check into difference instances as follows:
#include <check.h>
START_TEST (sanity_check1)
{
fail_unless(5 == 5, "this should succeed");
}
END_TEST
START_TEST (sanity_check2)
{
fail_unless(6 == 5, "this should fail");
}
END_TEST
START_TEST (sanity_check3)
{
ck_assert_str_eq("asa", "asasdfasdf");
}
END_TEST
int main(void)
{
Suite *s1 = suite_create("Core");
TCase *tc1_1 = tcase_create("Core");
SRunner *sr = srunner_create(s1);
int nf;
suite_add_tcase(s1, tc1_1);
tcase_add_test(tc1_1, sanity_check1);
tcase_add_test(tc1_1, sanity_check2);
tcase_add_test(tc1_1, sanity_check3);
srunner_run_all(sr, CK_ENV);
nf = srunner_ntests_failed(sr);
srunner_free(sr);
return nf == 0 ? 0 : 1;
}
and got the following output (displaying every failed test independently):
$ gcc script.c -Wall -o script -lcheck -pthread -pthread -lm
$ ./script
Running suite(s): Core
33%: Checks: 3, Failures: 2, Errors: 0
script.c:12:F:Core:sanity_check2:0: this should fail
script.c:18:F:Core:sanity_check3:0: Assertion '"asa" == "asasdfasdf"' failed: "asa" == "asa", "asasdfasdf" == "asasdfasdf"