Many of today's programming languages are based on C; like C++, C#, Java, Objective-C. So could I call a C method from C++ code? Or call C from Java or C#? Or is this goal out of reach and unreasonable? Please include a quick code sample for my and everyone else's understanding.
+4
A:
C++,C#, Objective-C, and Java can all call C routines. Here are a few links that will give you an overview of the process needed to call C from each language you asked about.
Alan
2010-06-13 07:50:38
Can it call C routines from just compiled C code. Or does the C code have to be in a library?
Mohit Deshpande
2010-06-13 07:52:20
C++ and Objective-C can compile C, as they are both supersets. C# and Java will require C code to exist in libs
Alan
2010-06-13 07:55:46
the article about java is not about calling c from java but calling java from c
ManBugra
2010-06-13 08:20:16
Whoops, copied the wrong like. Fixed.
Alan
2010-06-13 08:37:30
+2
A:
An example of calling C from C++. Save this C function in a file called a.c:
int f() {
return 42;
}
and compile it:
gcc -c a.c
which will produce a file called a.o. Now write a C++ program in a file called main.cpp:
#include <iostream>
extern "C" int f();
int main() {
std::cout << f() << std::endl;
}
and compile and link with:
g++ main.cpp a.o -o myprog
which will produce an execuatable called myprog which prints 42 when run.
anon
2010-06-13 07:59:05
A:
To Call C Methods In Java...
there a Keyword "native" in Which You can write machine-dependent C code and invoke it from Java....
Basically it creates a DLL file..then u have to load it in ur program...
a nice example here....
Vizay Soni
2010-06-13 08:52:06
A:
To call C methods from Java, there are multiple options, including:
- JNA - Java Native Access. Free. Easy to use. Hand-declaration of Java classes and interfaces paralleling existing C structs and functions. Slower than JNI - by a few hundred nanoseconds per call.
- JNI - Java Native Interface. Free. Fastest option. Requires a layer of native glue code between your Java code and the native functions you want to call.
- JNIWrapper - Commercial product, similar to JNA.
Andy Thomas-Cramer
2010-07-29 17:46:04