Python:代替gcc/g++ -o filename file 命令行 。

经常练习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()

转载于:https://www.cnblogs.com/nathaninchina/archive/2012/04/06/python_c_compile.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值