tags:

views:

289

answers:

7

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 languages 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++ ;) )

+11  A: 

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.

Christoph
He said "non-toy"
Terry Mahaffey
@Terry: Vala and Lisaac may be considered 'toy languages', but the others? If you're looking for languages which compile to C, you'll mostly find toy/research/domain-specific languages or compiler prototypes, as other languages will most likely in time get a compiler which compiles down to machine code or some intermediate representation of a compiler/vm kit
Christoph
+6  A: 

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.

Adam Rosenfield
D is not the only example of a language which can call C functions directly: others are Ada, Fortran, Pascal...; most compiled languages (as in statically compiled to machine code) can interface with C, but the details are compiler-specific
Christoph
Thanks, but I was actually kinda thinking about D as a couter-example of my question ;) . I tried using D to call SDL functions, but I lost the fight against Derelict (or more precisely, the half dozen build tools and D libraries that you have to use in order to get a working Derelict version). It's probably my fault, and it's a shame cause D look very interesting, but I just couldn't do in a few days what "extern(C) {" does in C++ ... :(
phtrivier
I never tried D, but http://www.digitalmars.com/d/1.0/interfaceToC.html seems pretty easy to me...
Christoph
@Christoph : yes it does look easy, and I tried it ... but then I realized some people found the need to write and maintain this kind of code http://www.dsource.org/projects/derelict/browser/branches/Derelict2/DerelictSDL/derelict/sdl/sdl.d ... and I definitely consider that wrapping ;) Not to mention the fact that I could not make it work, but I'm totally ready to attribute that to my own suckiness.
phtrivier
@phtrivier: wrappers are a necessary evil for languages which aren't either extensions of C (ie have a different syntax and/or type system) or don't allow for embedded C code; there's just no way around re-declaring the types/functions...
Christoph
He said "non-toy"
Terry Mahaffey
+2  A: 

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

That link describes the "old" way of connecting Fortran and C, in which you had to understand argument passing mechanisms, name mangling, etc. and the method was compiler dependent. The "new" way of using the Fortran ISO C Binding is easier and better. I've used both methods.
M. S. B.
+1  A: 

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

Roger Pate
It may not exactly fit the OP request, but +1 because ctypes is really good!
Francesco
+3  A: 

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.

slebetman
A: 

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,

Rob Wells
+3  A: 

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.

M. S. B.
This is in quite widespread corporate use for compatibility with older libraries (for math, finance, physics, weather...).
John Zwinck