C library function declarations

When implementing a language using LLVM as the backend, it is
necessary to give programs written in that language, access to the C
standard library functions. The Kaleidoscope tutorial shows how to do
this for individual functions using extern declarations, but in
general it would be necessary to have those predefined for the full
standard library. Presumably these would contain exactly the
information from the union of C header files. Is there a way, by
parsing the C header files or otherwise, to obtain this information in
the format LLVM expects?

As an extension of this question, it will be necessary to provide
access to other libraries written in C. The same question applies
there: Is there a way, by parsing C header files or otherwise, to
obtain a list of functions that are defined in a given library, in the
format LLVM expects?

Usually, this would be defined by the language. For instance, C# uses
PInvoke to declare functions that are implemented natively in some
other language, and the programmer usually manually writes PInvoke
declarations for whichever library functions he wants to call.

Many languages try to wrap the standard C libraries and expose
equivalent functionality through the language's own standard library.
This doesn't address third-party libraries, of course.

At any rate, if you want to consume native libraries in any language
without explicitly declaring each native function, you'll need to
parse the C header files. Native object files don't have information
about parameters, calling conventions, etc. that your compiler would

You may want to consider leveraging Clang to parse existing headers. It’s still not trivial to get everything working as you described, but by leveraging Clang at least you don’t have to parse C yourself.


Keir Mierle wrote: