What is the best way to invoke C / C ++ code from other languages ​​like Java, PHP, Perl, Python, etc.?

What is the best way to invoke C / C ++ from other languages ​​like Java, Python, Perl, PHP, etc.

+5
source share
4 answers

From perl

Inline :: C
Inline :: CPP
Inline :: Java
Inline :: Python
Inline :: Lua

excerpt from Inline :: C-Cookbook :

use Inline C => <<'END_C';

  void greet() {
    printf("Hello, world\n");
  }
END_C

greet;
+9
source

Use Swig, it allows you to generate code for several languages ​​that calls any C / C ++ function. http://www.swig.org/

+2
source

... c/++, , java, . , , .

+2

.

, C . Python:

os.system("myccode -v args etc")

, - , . , .

Each language has its own mechanism for calling C in a single process. Python, for example, has a C API, and you can create your C code in a Python extension. This allows a very tight integration, but it works better both when learning the C API and carefully writing code so that there is no memory leak.

Python also provides ctypes , which can call the C DLL. This is slightly simpler than the full C extension, but does not provide all the integration options.

+2
source

All Articles