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.

Eryk Sun
  • 29,588
  • 3
  • 76
  • 95
lads
  • 845
  • 2
  • 13
  • 26
  • 1
    Possible duplicate of [Calling an external command in Python](http://stackoverflow.com/questions/89228/calling-an-external-command-in-python) – Aran-Fey Jul 25 '16 at 15:38

1 Answers1

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.

rafaelleru
  • 314
  • 2
  • 15
  • 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 ? – lads Jul 25 '16 at 16:08
  • 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. – rafaelleru Jul 25 '16 at 16:16
  • 2
    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"`. – Eryk Sun Jul 25 '16 at 17:09