-3

How can I run c/c++ code within python in the form:

def run_c_code(code):
    #Do something to run the code
code = """
       Arbitrary code
       """
run_c_code(code)

It would be great if someone could provide an easy solution which does not involve installing packages. I know that C is not a scripting language but it would be great if it could do a 'mini'-compile that is able to run the code into the console. The code should run as it would compiled normally but this needs to be able to work on the fly as the rest of the code runs it and if possible, run as fast as normal and be able to create and edit variables so that python can use it. If necessary, the code can be pre-compiled into the code = """something""".

Sorry for all the requirements but if you can make the c code run in python then that would be great. Thanks in advance for all the answers..

Lasagnenator
  • 168
  • 2
  • 11
  • 2
    https://stackoverflow.com/questions/89228/calling-an-external-command-in-python it will help. – Karthikeyan.R.S Oct 11 '17 at 10:08
  • @Karthikeyan.R.S I am not trying to run code externally from python but running it from within python as the title says. It is a good answer but does not suit my purposes. – Lasagnenator Oct 11 '17 at 10:10
  • The problem is a bit too vague. What do you want to do with the code? Should it take as argument python objects? Or perhaps a numpy array? Should return somethign to python? – Ignacio Vergara Kausel Oct 11 '17 at 10:14
  • This was meant to be used for general purpose of running c code so I guess that it should be able to do everything that c can do, even take inputs. Surely there is a standard library file that allows you to do this? – Lasagnenator Oct 11 '17 at 10:17
  • Inlining C code is tricky, the only library that does something like that is `weave` but is kinda discontinued and working only for python 2.7. I found this example https://gist.github.com/adamnew123456/12546e6e36607a63e254 where it compiles on the fly the code but does not provide any communication between the c code and python and in the end is no different than compile and call it as a subprocess as in the first comment. – Ignacio Vergara Kausel Oct 11 '17 at 10:26
  • It is not possible to run C code within standard Python. You need to install additional libraries. The hard way could be using `distutils` to compile your 'code' at Python runtime ... – Maurice Meyer Oct 11 '17 at 10:28
  • @MauriceMeyer Good idea. I wouldn't mind if it compiles the c code at runtime, unfortunately, I would like the whole thing to be one file and not compiled c code outside the .py file – Lasagnenator Oct 11 '17 at 10:31
  • If you attempt to create this yourself, you could have a look at some C(++) script projects: https://stackoverflow.com/a/3153436/104774 I never tried it myself because I make the distinction: scripting=python, native=C++, but there might be use cases for this. – stefaanv Oct 11 '17 at 11:02

2 Answers2

1

As somebody else already pointed out, to run C/C++ code from "within" Python, you'd have to write said C/C++ code into an own file, compile it correctly, and then execute that program from your Python code.

You can't just type one command, compile it, and execute it. You always have to have the whole "framework" set up. You can't compile a program when you haven't yet written the } that ends the class/function/statement 20 lines later on. At this point you'd already have to write the whole C/C++ program for it to work. It's simply not meant to be interpreted on the run, line by line. You can do that with python, bash/dash/batch, and a few others. But C/C++ definitely isn't one of them.

With those come several issues. Firstly, the C/C++ part probably needs data from the Python part. I don't know of any way of doing it in RAM alone (maybe there is one, but I don't know), so the Python part would have to write it into a file, the C/C++ part would read and process it, then put the processed data into another file, and then the Python part would have to read that and continue.

Which brings another point up. Here we're already getting into multi-threading territory, because the moment you execute that C/C++ program you're dealing with a second thread. So, somehow, you'd have to coordinate those programs so that the Python part only continues once the C/C++ part is done. Shouldn't be a huge problem to get running, but it can be a nightmare to performance and RAM if done wrongly.

Without knowing to what extent you use that program, I also like to add that C/C++ isn't platform-independent like Python. You'll have to compile that program for every single different OS that you run it on. That may come with minor changes to the code and in general just a lot of work because you have to debug and test it for every single system.


To sum up, I think it may be better to find another solution. I don't know why you'd want to run this specific part in C/C++, but I'd recommend trying to get it done in one language. If there's absolutely no way you can get it done in Python (which I doubt, there's libraries for almost everything), you should get your Python to C/C++ instead.

Katembers
  • 103
  • 1
  • 1
  • 8
0

If you want to run C/C++ code - you'll need either a C/C++ compiler, or a C/C++ interpreter.

The former is quite easy to arrange (though probably not suitable for an end user product) and you can just compile the code and run as required.

The latter requires that you attempt to process the code yourself and generate python code that you can then import. I'm not sure this one is worth the effort at all given that even websites that offer compilation tools wrap gcc/g++ rather than implement it in javascript.

I suspect that this is an XY problem; you may wish to take a couple of steps back and try to explain why you want to run c++ code from within a python script.

UKMonkey
  • 6,941
  • 3
  • 21
  • 30
  • As a follow up note; you will have to pick which language you want - C or C++; they are not the same. – UKMonkey Oct 11 '17 at 11:39