C/Invoke

Easily call C from any language.

Information

C/Invoke is a library for connecting to C libraries at runtime. This differs from the typical method of interfacing with C, which involves writing static definitions which are then compiled to a machine-dependant format. C/Invoke provides a runtime facility to build descriptions of C functions and to call them, passing them appropriate data and retrieving results. C/Invoke provides a central repository of code to handle the platform-dependant details of marshaling C parameters and return values. Currently C/Invoke has been ported to GCC (GNU/Linux, *BSD) on the x86 and amd64 architectures, Microsoft Windows on x86, Mac OS X on PowerPC, and Solaris on SPARC. The development team is interested in adding more ports as they gain access to more diverse hardware.

Currently, bindings exist for the following languages or projects:

Audience

The main audience of C/Invoke are users of high-level programming languages who wish to interface with C. At the moment, most languages can be extended with C libraries only by authoring modules in C using language-specific APIs. There are many problems with this approach; writing extension modules requires knowledge of the C language instead of the language the application is being written in, the APIs needed to implement the modules are often involved and complex, and the resulting libraries must be built and maintained on many different platforms.

However, once C/Invoke is integrated into a language, the situation changes dramatically; users of Lua (for example) can interface to C by writing Lua code alone. This means that the advantages of Lua (or other high-level languages) such as portability and ease of use can be combined with the low-level power of C. To call a C function, one merely declares a C prototype in the high-level code, similar to the P/Invoke functionality of Microsoft's .NET Framework. A subroutine is then created in the high-level environment which hooks into a proxy generated to correctly call the given C function.

There are two ways to integrate C/Invoke into a language. One is to use the existing extension module interface of the language in the normal manner. This is the preferred method to integrate with a language which is already established and which would be hard to modify, such as Perl, Python, Ruby, Tcl, etc. The other way would be to link C/Invoke directly into the language runtime and make the declarative C services part of the syntax of the language.

The C/Invoke developers are always looking for volunteers to integrate with an existing language, or programming language authors who are interested in using C/Invoke directly.

Development

C/Invoke is Free Software and is open for both development and usage by anyone. It is licensed under a "Modified BSD" scheme in order to allow the greatest possible audience of high-level languages to use it, because the licensing terms of these languages cannot always be predicted.

This web space and the development project space have been generously provided by the GNU Savannah Project. You can visit that site by clicking on the C/Invoke Savannah Project link on the sidebar. There you can submit a support issue such as a bug, or join the mailing lists to talk with the C/Invoke developers directly.

The source code is not hosted in the Savannah CVS repository because the C/Invoke developers greatly prefer Subversion. The URL for the subversion repository is:

svn://pleep.com/trunk/cinvoke

This repository is open to the public for read-only access.

If you would like to help develop C/Invoke by porting it to a new architecture, fixing bugs, writing a language binding, or otherwise, please join our development mailing lists. At the moment there are two lists, one for all usage and development discussion and one which sends SVN update e-mails. Please send all correspondance relating to C/Invoke to the cinvoke-dev list. Information on the lists and instructions for joining can be found on the Savannah mailing list page.