0

I am trying to make a program in c++, but i cant make the program because in one part of the code I need to run a python program from c++ and I dont know how to do it. I've been trying many ways of doing it but none of them worked. So the code should look sometihnglike this:somethingtoruntheprogram("pytestx.py"); or something close to that. Id prefer doing it without python.h. I just need to execute this program, I need to run the program because I have redirected output and input from the python program with sys.stdout and sys.stdin to text files and then I need to take data from those text files and compare them. I am using windows.

2
  • 3
    #include <windows.h> and then you have choice of doing system('py program.py') or CreateProcess() or ShellExecute() and many more. This honestly is very broad topic and each method has it's own advantages and problems. Commented May 17, 2017 at 11:47
  • the POSIX way to do it is with fork/exec: en.wikipedia.org/wiki/Exec_(system_call) Commented May 17, 2017 at 11:49

2 Answers 2

3

You have two way of doing that:

Using a embedded interpreter is (IMHO) the best way to do it because it gives you more control over the execution of the script, because it's not OS-dependant and it does not rely on your target having a python interpreter (configured as you require).

Sign up to request clarification or add additional context in comments.

2 Comments

yes, I linked the 2.6 doc for python because that's the first link that I found, but every python version has a way (more or less identical to the other version) to embed an interpreter in C code. Boost add a C++ wrapper. Both boost and python work for the OS where python is ported.
Cython is also worth mentioning, especially if you're looking for perfomance.
0

There's POSIX popen and on Windows _popen, which is halfway between exec and system. It offers the required control over stdin and stdout, which system does not. But on the other hand, it's not as complicated as the exec family of functions.

Comments

Your Answer

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.