Stddef.h not found, size_t not defined using libclang

Hello, I’m trying to figure out the right set of flags to pass to libclang C API to get it to parse a simple file correctly.

I have the clang+llvm package installed on my Ubuntu 20.04 (under WSL2) to:

/home/anders/packages/llvm/14.0.0

and I’m trying to inspect the AST for this simple example:

#include <stddef.h>

const size_t a = 0;

If I just pass the following arguments to clang_parseTranslationUnit():

        "-std=c++14",
        "-x", "c++",

then I get the following error:

testdata/test_size_t.cpp:1:10: fatal error: 'stddef.h' file not found

Ok, no problem, I’ll add the c++ includes from the clang installation:

        "-std=c++14",
        "-x", "c++",
        "-isystem/home/anders/packages/llvm/14.0.0/include/c++/v1/", 
        "-isystem/home/anders/packages/llvm/14.0.0/include/x86_64-unknown-linux-gnu/c++/v1/", 

This now gives me:

/home/anders/packages/llvm/14.0.0/include/c++/v1/stddef.h:45:15: fatal error: 'stddef.h' file not found

I have two other stddef.h’s on my system, one in /usr/include/linux' and another in /usr/lib/gcc/x86_64-linux-gnu/9/include. The one in in /usr/include/linux` is empty, so adding that to the include path gives me:

testdata/test_size_t.cpp:3:7: error: unknown type name 'size_t'

And the one in /usr/lib/gcc... appears to work (at least no more diagnostic errors), but it feels like I’m mixing and matching clang and gcc here. My final set of args is:

        "-std=c++14",
        "-x", "c++",
        "-isystem/home/anders/packages/llvm/14.0.0/include/c++/v1/", 
        "-isystem/home/anders/packages/llvm/14.0.0/include/x86_64-unknown-linux-gnu/c++/v1/", 
        "-isystem/usr/lib/gcc/x86_64-linux-gnu/9/include/"

Are there any more I should have as a “base” set before adding project-specific ones? are these even correct?

And if I try to include something that includes I get a whole bunch of different errors. Is there a guide somewhere to setting up include paths correctly?

/home/anders/packages/llvm/14.0.0/include/c++/v1/cmath:572:12: error: reference to unresolved using declaration
/home/anders/packages/llvm/14.0.0/include/c++/v1/cmath:592:12: error: reference to unresolved using declaration
/home/anders/packages/llvm/14.0.0/include/c++/v1/cmath:612:12: error: reference to unresolved using declaration
/home/anders/packages/llvm/14.0.0/include/c++/v1/stdlib.h:111:39: error: declaration conflicts with target of using declaration already in scope
/home/anders/packages/llvm/14.0.0/include/c++/v1/stdlib.h:114:44: error: declaration conflicts with target of using declaration already in scope
/home/anders/packages/llvm/14.0.0/include/c++/v1/stdlib.h:120:40: error: declaration conflicts with target of using declaration already in scope
/home/anders/packages/llvm/14.0.0/include/c++/v1/stdlib.h:124:41: error: declaration conflicts with target of using declaration already in scope
/home/anders/packages/llvm/14.0.0/include/c++/v1/stdlib.h:129:1: error: declaration conflicts with target of using declaration already in scope
/home/anders/packages/llvm/14.0.0/include/c++/v1/stdlib.h:148:34: error: unknown type name 'ldiv_t'
/home/anders/packages/llvm/14.0.0/include/c++/v1/stdlib.h:149:12: error: no member named 'ldiv' in the global namespace
/home/anders/packages/llvm/14.0.0/include/c++/v1/stdlib.h:152:34: error: unknown type name 'lldiv_t'
/home/anders/packages/llvm/14.0.0/include/c++/v1/stdlib.h:154:12: error: no member named 'lldiv' in the global namespace
/home/anders/packages/llvm/14.0.0/include/c++/v1/math.h:792:46: error: declaration conflicts with target of using declaration already in scope
/home/anders/packages/llvm/14.0.0/include/c++/v1/math.h:792:93: error: no member named 'acosf' in the global namespace
/home/anders/packages/llvm/14.0.0/include/c++/v1/math.h:793:46: error: declaration conflicts with target of using declaration already in scope
/home/anders/packages/llvm/14.0.0/include/c++/v1/math.h:793:93: error: no member named 'acosl' in the global namespace
/home/anders/packages/llvm/14.0.0/include/c++/v1/math.h:799:1: error: declaration conflicts with target of using declaration already in scope
/home/anders/packages/llvm/14.0.0/include/c++/v1/math.h:799:40: error: reference to unresolved using declaration
/home/anders/packages/llvm/14.0.0/include/c++/v1/math.h:804:46: error: declaration conflicts with target of using declaration already in scope

After some experimentation with the output of clang -### the answer appears to be -resource-dir ${LLVM_ROOT}/lib/clang/X.X.X