cmacosgccc-criterion

Criterion C header not found on macOS


I have the requirement to install Criterion (which is a testing framework for C) on my laptop running macOS. I use Homebrew daily so I thought it will be be easy to just run the brew command:

brew install criterion

But, every time I try to run a C file with the criterion header with GCC or CLANG, it just can't find Criterion, although it is installed. (I use the simple sample from the official Criterion repo)

#include <criterion/criterion.h>

Test(misc, failing) {
    cr_assert(0);
}

Test(misc, passing) {
    cr_assert(1);
}
$ gcc -o test test.c -lcriterion
test.c:1:10: fatal error: 'criterion/criterion.h' file not found
#include <criterion/criterion.h>
         ^~~~~~~~~~~~~~~~~~~~~~~
1 error generated.
$ brew install criterion
Warning: criterion 2.4.1_1 is already installed and up-to-date.
To reinstall 2.4.1_1, run:
  brew reinstall criterion

I'm struggling with that for almost 2 days and I'm out of ideas... I even tried to install gcc via Homebrew but it doesn't seem to work either

$ /opt/homebrew/Cellar/gcc/12.2.0/bin/gcc-12 -o test test.c -lcriterion
test.c:1:10: fatal error: criterion/criterion.h: No such file or directory
    1 | #include <criterion/criterion.h>
      |          ^~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.

I use the Apple clang version 14.0.0 for gcc and the homebrew gcc uses gcc 12.2.0. I run a MacBook Air M1 2020 with Ventura 13.1 (22C65)


Solution

  • I had a very similar problem and as @Rup pointed out, the solution is to pass Homebrew's lib and include folder, which contain the binaries and header files respectively, to the C Compiler/Linker.

    Makefile example:

    test: test.c
        gcc $< -o $@ -I/opt/homebrew/include -L/opt/homebrew/lib -lcriterion
    

    Or just run in the terminal:

    gcc -o test test.c -I/opt/homebrew/include -L/opt/homebrew/lib -lcriterion