Skip to content

Instantly share code, notes, and snippets.

@nkmathew
Last active January 18, 2018 07:37
Show Gist options
  • Save nkmathew/8183548 to your computer and use it in GitHub Desktop.
Save nkmathew/8183548 to your computer and use it in GitHub Desktop.
A script for running single C/C++ files from SciTE
#!/usr/bin/python2.7
import os
import sys
import platform
import subprocess
import re
"""
This program runs gcc/g++ on a cpp/c source file. It supposed to be used in
SciTE in a portable way. A bash script can do what it does. The same can't
be said for a .bat file.
+ It's able to handle sourcefiles with spaces in them
+ Runs the appropriate compiler depending on the extension of the files
+ Generates the right executable name irregardless of the position of
the '-o' option.
"""
# Surround all arguments in double quotes since the quotes will
# be removed in sys.argv
arg_list = map(lambda x:'"' + x + '"', sys.argv)
OS = platform.system()
exe_name = ""
prev_arg = ""
compiler = ""
# Determine compiler to be used and the name of the out executable
# file.
for arg in sys.argv:
if arg.endswith(".c"):
compiler = "gcc "
elif re.search("\.(cpp|cc|cxx|C)$", arg):
compiler = "g++ "
if prev_arg == "-o":
exe_name = arg
prev_arg = arg
if exe_name == "":
## No outfile found
if OS == "Linux":
exe_name = "a.out"
else:
exe_name = "a.exe"
command = compiler + " ".join(arg_list[1:])
try:
os.remove(exe_name)
except:
pass
retval = os.system(command)
if retval == 0 and os.path.exists(exe_name):
exe_name = os.path.abspath(exe_name)
subprocess.call('"' + exe_name + '"')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment