经常练习C语言,所有有许多的源代码需要编译。但是每次都要用到 -o 加上文件名,比较麻烦,所有用python编了一个程序完成整体的操作,不需要 -o filename 部分,可以使用 -g参数。这个程序还不能支持多文件编译。这个功能会不久后加上。
#!/usr/bin/env python """ File: gg.py ----------------- create single/multi c/c++ source file(s) the output has the same name with the source file so you can compile c/c++ source file without -o """ import os import sys import getopt def my_compile(filepath): if not os.path.isfile(filepath): print "the source file does not exists!" sys.exit(2) file_ext=filepath.rpartition(".")[2] cmd=filepath.rpartition(".")[0] if file_ext=='c': os.system("gcc -g -o %s %s && %s" % (cmd, filepath, cmd)) if file_ext in ('C', 'cpp'): os.system("g++ -g -o %s %s && %s" % (cmd, filepath, cmd)) def _usage(): print "usage: ", "[-g] filename" sys.exit(2) def main(): try: opts, args=getopt.getopt(sys.argv[1:], 'h') except getopt.GetoptError, err: sys.stderr=err _useage() # check opts for opt in opts: if '-h' in opt: _usage() # check args filepath=args[0] if os.path.dirname(filepath) is '': filepath=os.path.join("./", filepath) my_compile(filepath) # file will be checked in my_compile function if __name__=='__main__': main()