I am using libclang to parse C source files using a compilation database. Overall, I am pretty happy with the results, but I have run into an issue that I don’t know how to solve.
Given an example program like:
size_t hello = 5;
libclang will complain that
fatal error: 'stddef.h' file not found. Doing some digging I find clang has it in on my Fedora 36 system as
/usr/lib64/clang/14.0.5/include/stddef.h. How is one supposed to access this directory without hardcoding it? I thought maybe it would be include as a CMake variable that one could access, but that has not been my experience unless I am missing it. Somehow clangd is able to resolve this appropriately, so I must be missing something extremely obvious.
> clang -print-resource-dir
will maybe print
Yep, this is working on my system. I will roll with it for now, but surely there is a better way.
-MJ option gets you one step further:
clang -MJ foo.json -c foo.c -o foo.o
foo.json describes the compilation.
I don’t have much familiarity with libclang, but I can provide an answer from a clangd perspective:
The place where clangd picks up clang’s default built-in include paths is the
createInvocation() call here. This function is documented to, among other things, “prob[e] the system for settings like standard library locations”.
Since libclang is a C API wrapper, you would not be calling
createInvocation() directly, but a libclang API that calls it in its implementation (or which calls some code also called by
createInvocation()). I don’t know off the top of my head what libclang API you need to use, but that’s a potential direction to look.
This line really makes me think that there is no good way to do what I am trying to do. Using the binary to probe for paths?
This was definitely a great pointer.
I think the best solution is probably what @tschuett said for now. I did comment on the llvm pkg-config issue though, since that would be the best solution.
Can you share what libclang API / calls you’re using?
I feel like libclang should also give you an opportunity to specify a compiler command line, similar to