开发者

Languages with direct C compatibilty

Apart from C++, which non-toy languages have direct or easy-to-use compatibility to C? As in "I can take a C library out there, and compile my code against it without having to find, write, or configure some kind of wrapper."

I know that lots of languages have compatibility with C through some form of external call or binding (I've been using bindings in Java, Ruby, Python, etc., so I know it can be done). But you rely on someone (possibly you), to write and maintains a binding for all the libraries you want to use, and the binding has to works on all platforms, etc.

Do more expressive l开发者_如何学运维anguages than C++ have this feature?

Thanks to all for the mentions of swig or related wrapper-generation tools. I am aware that those exists, but I don't think they're really as easy as C->C++ integration... but then integrating with C might be the only thing that is easier in C++ ;) )


Objective-C, the bastard child of C and Smalltalk.

Objective-C is a direct superset of C (you can't get more compatible than that), but there are languages which compile to C. Some recent examples would be Vala and Lisaac.

Most statically compiled languages allow interfacing with C libraries. Examples of such languages are Ada, Fortran, Pascal and D. The details are platform and compiler specific; for x86, this basically means supporting the cdecl calling convention.


The D language is compatible with the C ABI. However, it's sometimes non-trivial to convert C header files into compatible D modules. See this page for more details.


Fortran can call C routines, or be called by C. This used to be "platform and compiler specific" as stated in another answer, but Fortran 2003 includes the "ISO C Binding", which makes this part of the language standard and therefore portable rather than platform and compiler specific. The ISO C Binding is supported by numerous Fortran compilers, including gfortran (>= 4.3), Intel ifort, Sun Fortran, etc.

You do have to write an "interface" description of a C routine being called, but it is compiler and platform independent.


For a lot of languages, wrapper code for C libraries are not hard to write - just use SWIG: http://www.swig.org/

While originally written as a quick wrapper generator for Tcl, it now supports: Tcl, Python, Perl, Guile, Java, Ruby, Scheme, PHP, Ocaml, Pike, C#, Modula-3, Lua, Common Lisp, R and Octave.

If you use any of the language it supports, give it a try. For C functions that deals with strings, integers and floats it is very easy to use. For more complex C functions it obviously gets more complex.


With the right compilers/linkers, name mangling and function arguments, you can link C and Fortran modules. Details here.


Python has a dynamic wrapping module, ctypes, which, while it doesn't eliminate boilerplate binding code completely, does greatly reduce it.


G'day,

You can interface Perl onto C libraries by writing an XS interface between the two. Here's the perlXSTut over at CPAN.

This is how the XML::LibXML and XML::LibXSLT modules are implemented.

You can also interface Ada to C libraries buy means of pragma Import. This is also true for libraries written in C++. And COBOL and FORTRAN BTW.

HTH

cheers,

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜