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.
今天的许多编程语言都基于C语言;比如c++, c#, Java, Objective-C。我可以从c++代码中调用C方法吗?还是用Java或c#来调用C ?还是这个目标遥不可及且不合理?请包含一个快速代码示例,以供我和其他人理解。
5 个解决方案
#1
5
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.
c++、c#、Objective-C和Java都可以调用C例程。这里有一些链接,可以让你了解你所询问的每一种语言需要调用C的过程。
- Calling C from Java
- 从Java调用C
- Calling C from C++
- 调用C与c++
- Calling C from C#
- 从c#调用C
- Calling C from Objective-C
- 从objective - C调用C
#2
2
An example of calling C from C++. Save this C function in a file called a.c:
从c++调用C的一个例子。将这个C函数保存在一个叫做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:
它将生成一个名为a.o的文件。现在在一个名为main.cpp的文件中编写一个c++程序:
#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.
这将产生一个可执行的称为myprog的程序,它在运行时打印42。
#3
1
To Call C Methods In Java...
在Java中调用C方法……
there a Keyword "native" in Which You can write machine-dependent C code and invoke it from Java....
有一个关键字“本地”中,您可以编写计算机有关C代码和调用它从Java ....
Basically it creates a DLL file..then u have to load it in ur program...
基本上它会创建一个DLL文件。然后你必须把它加载到你的程序中……
a nice example here....
一个很好的例子....
#4
0
To call C methods from Java, there are multiple options, including:
要从Java调用C方法,有多个选项,包括:
- 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.
- JNA - Java本地访问。免费的。易于使用。Java类和接口的手工声明与现有的C结构体和函数并行。比JNI慢——每次调用要慢几百纳秒。
- 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.
- JNI - Java本机接口。免费的。最快的选择。在Java代码和要调用的本机函数之间需要一层本机胶水代码。
- JNIWrapper - Commercial product, similar to JNA.
- JNIWrapper -商业产品,类似于JNA。
#5
0
To call C/C++ from Java, also give a look at BridJ (it's like JNA with C++ and better performance).
要从Java调用C/ c++,还可以查看BridJ(它就像JNA具有c++和更好的性能)。
#1
5
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.
c++、c#、Objective-C和Java都可以调用C例程。这里有一些链接,可以让你了解你所询问的每一种语言需要调用C的过程。
- Calling C from Java
- 从Java调用C
- Calling C from C++
- 调用C与c++
- Calling C from C#
- 从c#调用C
- Calling C from Objective-C
- 从objective - C调用C
#2
2
An example of calling C from C++. Save this C function in a file called a.c:
从c++调用C的一个例子。将这个C函数保存在一个叫做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:
它将生成一个名为a.o的文件。现在在一个名为main.cpp的文件中编写一个c++程序:
#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.
这将产生一个可执行的称为myprog的程序,它在运行时打印42。
#3
1
To Call C Methods In Java...
在Java中调用C方法……
there a Keyword "native" in Which You can write machine-dependent C code and invoke it from Java....
有一个关键字“本地”中,您可以编写计算机有关C代码和调用它从Java ....
Basically it creates a DLL file..then u have to load it in ur program...
基本上它会创建一个DLL文件。然后你必须把它加载到你的程序中……
a nice example here....
一个很好的例子....
#4
0
To call C methods from Java, there are multiple options, including:
要从Java调用C方法,有多个选项,包括:
- 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.
- JNA - Java本地访问。免费的。易于使用。Java类和接口的手工声明与现有的C结构体和函数并行。比JNI慢——每次调用要慢几百纳秒。
- 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.
- JNI - Java本机接口。免费的。最快的选择。在Java代码和要调用的本机函数之间需要一层本机胶水代码。
- JNIWrapper - Commercial product, similar to JNA.
- JNIWrapper -商业产品,类似于JNA。
#5
0
To call C/C++ from Java, also give a look at BridJ (it's like JNA with C++ and better performance).
要从Java调用C/ c++,还可以查看BridJ(它就像JNA具有c++和更好的性能)。