How to run c code within python

Question:

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..

Asked By: Lasagnenator

||

Answers:

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.

Answered By: UKMonkey

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.

Answered By: Katembers

visit https://www.youtube.com/watch?v=zoX7N3_DbcE

How to run C code from Python Program?

Answered By: Divyesh Sethiya
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.