[LLVMdev] C library function declarations

Nick Lewycky nicholas at mxc.ca
Mon Jan 4 18:54:03 PST 2010

Keir Mierle wrote:
> On Mon, Jan 4, 2010 at 5:41 AM, Kenneth Uildriks <kennethuil at gmail.com 
> <mailto:kennethuil at gmail.com>> wrote:
>     On Mon, Jan 4, 2010 at 4:43 AM, Russell Wallace
>     <russell.wallace at gmail.com <mailto:russell.wallace at gmail.com>> wrote:
>     > 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
>     need.
> 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.
I can't test it right now, but I believe "clang -femit-all-decls" will 
do it.


More information about the llvm-dev mailing list