0

How can I run the following command from Python3 on Windows 7

gcc main.cpp -o main.out 
./main.out

The purpose is to execute the main.cpp file from Python3.

1

1 Answer 1

1

look subprocess call

import subprocess
subprocess.run(["gcc", "main.cpp -o main.out"])
subprocess.run(["./main.out"])

should work. But subprocess have more utilities that will be usefull for you.

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

3 Comments

Thanks for the answer. But I have a compile time error, because I need to link the some .lib files.I can compile in VS because I add the .lib files in the Linker-> Input. So do you know how can I see the cmd Visual Studio uses to compile and execute my code ?
Sorry but I alwais use Linux for programing and I only know the basics about cmd. But subprocces module have options to manage the output, and yo can link the libs withs gcc from the cmd, try -I or -l option.
The argument needs to be either ["gcc", "main.cpp", "-o", "main.out"] or "gcc main.cpp -o main.out". Otherwise subprocess.list2cmdline creates the incorrect command line gcc "main.cpp -o main.out".

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.