I have a Python code and, after running all the stuff in it, I want it to simply run a c++ file I have in the same directory. I read about Cython and BoostPython, but I don't think it is what I need (I could be wrong, obviously). I don't want to call functions, simply run the c++ algorithm. Is there an easy way to do it?
asked Jan 20, 2020 at 18:40
-
Do you need to share data between python and the c++ program?snek_case– snek_case2020年01月20日 18:43:34 +00:00Commented Jan 20, 2020 at 18:43
-
2when you say "c++ file" do you mean a source file or an executable?463035818_is_not_an_ai– 463035818_is_not_an_ai2020年01月20日 18:53:23 +00:00Commented Jan 20, 2020 at 18:53
-
no, @snek_case, I just want to run the c++ program.donut– donut2020年01月20日 18:59:04 +00:00Commented Jan 20, 2020 at 18:59
-
@formerlyknownas_463035818 the source file.donut– donut2020年01月20日 18:59:18 +00:00Commented Jan 20, 2020 at 18:59
-
1You will need to compile (translate) the Source C++ file into an executable before you can run it. You can have Python run a compiler and specify your program (along with other command line parameters), then have Python run the executable program. An alternative is to find or create an interpreter for C++.Thomas Matthews– Thomas Matthews2020年01月20日 19:27:33 +00:00Commented Jan 20, 2020 at 19:27
1 Answer 1
You can try open it as a subprocess in your script like this:
import subprocess
subprocess.call(["g++", "hello_world.cpp"])
tmp=subprocess.call("./a.out")
print("printing result")
print(tmp)
answered Jan 20, 2020 at 18:45
Comments
default