I have been writing a simple python script to compile and run a given .cpp file.
I've gotten it to work as expected but I want to be able to stop the script if the compilation of the .cpp file produces errors or warnings, rather then going on to execute the .exe file.
# run_cpp.py
# Script that compiles and executes a .cpp file
# Usage:
# python run_cpp.py -i <filename> (without .cpp extension)
import sys, os, getopt
def main(argv):
cpp_file = ''
exe_file = ''
try:
opts, args = getopt.getopt(argv, "hi:",["help",'ifile='])
except getopt.GetoptError as err:
# print help information and exit
print(err)
usage()
sys.exit(2)
for o, a in opts:
if o in ("-h", "--help"):
usage()
sys.exit()
elif o in ("-i", "--ifile"):
cpp_file = a + '.cpp'
exe_file = a + '.exe'
run(cpp_file, exe_file)
def usage():
print('run_cpp.py -i <filename> (without .cpp extension)')
def run(cpp_file, exe_file):
os.system("echo Compiling " + cpp_file)
os.system('g++ ' + cpp_file + ' -o ' + exe_file)
os.system("echo Running " + exe_file)
os.system("echo -------------------")
os.system(exe_file)
if __name__=='__main__':
main(sys.argv[1:])
When I run the following command on windows MINGW, everything is great.
$ python run_cpp.py -i hello
Compiling hello.cpp
Running hello.exe
-------------------
Hello, world!
where hello.cpp is :-
#include <iostream>
using namespace std;
int main() {
cout << "Hello, world!" << endl;
return 0;
}
If I leave out the semicolon after endl, the script goes on to run the .exe file, despite the compile errors.
$ python run_cpp.py -i hello
Compiling hello.cpp
hello.cpp: In function 'int main()':
hello.cpp:8:5: error: expected ';' before 'return'
return 0;
^~~~~~
Running hello.exe
-------------------
Hello, world!
How can I make my script know whether or not compilation was successful before trying to run the .exe file?
I've updated the code based on the answer.
if os.system('g++ ' + cpp_file + ' -o ' + exe_file) == 0:
os.system(exe_file)
Now the script will only execute the .exe file when compilation succeeds.
Another way to do the same thing but using the subprocess library instead of the os library, as recommended by the python documentation would be -
def run(cpp_file, exe_file):
x = subprocess.getoutput('g++ ' + cpp_file + ' -o ' + exe_file)
if x == "": # no error/warning messages
subprocess.run(exe_file) # run the program
else:
print(x) # display the error/warning