1

You know, we can bind every C library on Python or Perl programming languages. A good instance is PyQt; PyQt is bound from Qt.

My question is: Can I do the reverse of the above? I mean: suppose I have a library in Python or Perl, and I want to convert it to a C library...can it be done? However, you can think to convert a web program to shared library or a set of functions.

My Goal: I want to improve a set of security features.

Dharman
  • 30,962
  • 25
  • 85
  • 135
PersianGulf
  • 2,845
  • 6
  • 47
  • 67
  • My question to you is, why do you want to go through all the work of translating the program to modify it? Why not just modify these security features in the original language? – gankoji Dec 24 '14 at 05:17
  • I'm thinking to a master of sience project to eliminate web server, of course it's an idea.... – PersianGulf Dec 24 '14 at 05:21
  • So people write their code in python/perl/php, and send it to your 'server' and you run it in C? something like that? – gankoji Dec 24 '14 at 05:51

2 Answers2

4

Yes, you can. The term of art is "embedding," as in "embedded Python" or "embed a Python interpreter." Python has a document about it here: https://docs.python.org/2/extending/embedding.html - the general idea is you must use (at least a small part of) the Python C API to launch Python within a C or C++ application.

Once you realize you can embed a scripting language in C, and that scripting languages can invoke C, you then realize you can also embed one scripting language in another, using C as the bridge between them. For example, RubyLuaBridge: https://bitbucket.org/neomantra/rubyluabridge

A lot of commercial applications embed a scripting language interpreter within a C or C++ host program. A good, well-documented example is Adobe Lightroom, which is roughly half C++ and half Lua. You can read about that from the horse's mouth starting on page xi here: http://www.lua.org/gems/front.pdf

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
1

Yes, for python at least: Convert Python program to C/C++ code?.

And for Perl: http://perldoc.perl.org/5.8.9/perlcompile.html.

[EDIT] Per the comment, I'll expand. First, the question "Can I translate Python to C?" has already been answered on SO. See the link.

Second, Perl is actually an interpreted language (just like Python), and has the capability to take that intermediate code and translate it into full blown C for native executables. This is done using the 'B' module and it's other companion modules, such as B::C. There's also a standalone program, 'perlcc' for doing just this. [/EDIT]

Community
  • 1
  • 1
gankoji
  • 853
  • 1
  • 10
  • 23
  • Do you know about PHP? – PersianGulf Dec 24 '14 at 05:16
  • True, I was just being lazy. Although, in the case of both the PHP and Python links, they are to other questions asked on stackoverflow, which generally aren't re-answered, hence the links. – gankoji Dec 24 '14 at 05:52