views:

559

answers:

16

I have experience with OCaml. You had to write a stub for every function you wanted to use to convert the types even C int <-> OCaml int. Linking was painful a well. I don't even want to thing about mapping C++ objects.

What about other popular languages? Is it always a pain?

EDIT:

Please avoid duplicates. And state C and C++ interfacing capabilities separately.

EDIT 2:

Please be specific. "X can call C" doesn't give too much information.

+15  A: 

Python has a very good C API. It can be integrated to C++ also very easily and conveniently using the boost::python C++ binding for the Python C API.

Sahasranaman MS
You should refer to the ctypes module and http://docs.python.org/extending/extending.html
Novelocrat
And pyrex.
Paul Biggar
A: 

Interfacing with C++ objects will always be painful, as there's no standard ABI (Binary interface).

milan1612
Well. I guess you are talking about virtual functions. You can do a lot without them.
Łukasz Lew
As long as the interface you use deals with calling conventions, layout, etc, rather than forcing you, the programmer, to do so, it should be fine.
Novelocrat
If you can interface with ELF you should be able to interface with C++ objects relatively easily.
Imagist
+3  A: 

Python has a great C interface using built-in ctypes module. In order to interface C++ modules, SWIG can be used.

Roman Zeyde
+8  A: 

There are varying degrees of enjoyment available among mainstream languages and their C interfaces. Gladly, you can use SWIG for most of them.

Vinko Vrsalovic
+3  A: 

Python is a powerful and easy to learn high level language and has good documentation of extending it with C and C++:

http://docs.python.org/extending/extending.html

Using this approach is simple, but you'd write your extensions explicitly for Python. Using SWIG (see Extending Python with C++ for a nice little tutorial), you create the C/ C++ code as if it was to be run by itself plus an interface file that SWIG takes to create some wrapping code for you that you can use in Python (or other languages, for that matter).

Gerald Senarclens de Grancy
Isn't Boost.Python considered more the thing these days?
KayEss
+1  A: 

Java has a nice native interface with JNI, C# has something very similar.

Noon Silk
JNI is far to be nice... try JNA
dfa
Oh, apologies if JNI isn't the 'in' thing anymore; It's been 8 years since I last did Java :P
Noon Silk
Can you be more specyfic?
Łukasz Lew
@lukasz in what sense? In JNI (and probably JNA) and C# you just write the header of the function as 'extern ...' and then you call it appropriately. What else are you looking for?
Noon Silk
@Lukasz: I think that @dfa is alluding to the fact that JNI is hard to use. If you make a mistake using JNI you risk crashing the JVM.
Stephen C
Well I never used JNI.The standard issues are: - basic parameter type compatybility - more complex types like structs - overloading - object passing - variable argument functions etcLink at least would be usfull.
Łukasz Lew
@Lukazs: For example, if your C / C++ is not thread-safe (e.g. it calls some non-thread-safe library function), then you must make sure that you only call it via JNI from one Java thread.
Stephen C
I don't know for sure, but when using JNI, aren't there issues with endian-ness? That is, Java uses big endian while the most common platforms (and therefore, C on the most common platforms) use little endian.
Imagist
+6  A: 

Nearly all of the scripting languages (Perl, Python, Lua, PHP, Ruby, Tcl) are designed to be embedded into C and C++.

A good survey paper of the relative merits of the APIs:

H. Muhammad and R. Ierusalimschy. C APIs in extension
and extensible languages. Journal of Universal Computer
Science, 13(6):839–853, 2007.

See also this very similar question (and my answer in particular ;)).

Paul Biggar
I love the syntax highlighting :)
Skilldrick
Skilldrick: Me too. I wonder why it does that: http://meta.stackoverflow.com/questions/14664/how-does-so-syntax-highlight-citations-so-well
Paul Biggar
A: 

I linked FORTRAN libraries in once. I'd tell you more, but it's time for my nap.

A: 

Tcl can call C/C++ code

dimba
+2  A: 

You might like this recent comp.compilers thread about various foreign-function interfaces. It was suggested that Haskell had one of the nicest.

Paul Biggar
A: 

D is designed to be easily intefaced to C.

D 2.0 does have a limited interface to C++ code.

Łukasz Lew
+1  A: 

R is extensible via C, C++ and Fortran, this is described in the R Extensions Manual and the contributed Rcpp package makes it easier to call C++ functions.

Dirk Eddelbuettel
+4  A: 
Norman Ramsey
SWIG can be a pain, but it has the benefit of just having to learn it once.
Vinko Vrsalovic
A: 

Lua's integration abilities with C (and by extension C++) are absolutely first class. Its a wonderful little language, too. I don' think it gets nearly the love it deserves.

link: http://www.lua.org/

Shaun
A: 

Common Lisp implementations will often have a FFI that works really well with C. The nice thing is that you don't have to write any C code to use C libraries; all you need to do is write the declaration of the C function is Lisp.

Factor has copied this FFI system from Lisp.

Brian
+1  A: 

Falcon is a good alternative to Python in this case : http://falconpl.org

Klaim