0

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
6
  • Do you need to share data between python and the c++ program? Commented Jan 20, 2020 at 18:43
  • 2
    when you say "c++ file" do you mean a source file or an executable? Commented Jan 20, 2020 at 18:53
  • no, @snek_case, I just want to run the c++ program. Commented Jan 20, 2020 at 18:59
  • @formerlyknownas_463035818 the source file. Commented Jan 20, 2020 at 18:59
  • 1
    You 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++. Commented Jan 20, 2020 at 19:27

1 Answer 1

6

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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.