Python实现生成简单的Makefile文件代码示例


Posted in Python onMarch 10, 2015

在linux下写几个测试程序,还要一行行的输入g++命令进行编译,当经常改测试代码的时候,那一次次的敲(或者一次次的上线箭头选)也感觉不爽,不如make来的快。用Makefile的好处就不用多说了,这里我写了个脚本,其功能是自动搜索当前目录(不包括子目录)下的“.c”文件生成Makefile文件。

代码在这里,功能有限(适用于单个文件是一个独立的测试代码的情况),需要的朋友可以稍作修改以满足需求。

#! /usr/bin/python

'''

 File      : genMakefile.py

 Author    : Mike

 E-Mail    : Mike_Zhang@live.com

'''

import os
def genMakefileStr(dir,surfix = '.c'):

    msg = ''

    msg = msg + 'CC = gcc' + '\n'

    msg = msg +  'CFLAGS = -g -O2 -Wall' + '\n\n'

    

    fList = []

    for dirPath,dirNames,fileNames in os.walk(dir):

        for file in fileNames:

            name,extension = os.path.splitext(file)

            if extension == surfix:

                fList.append(name)

        break # only search the current directory

    str1 = 'all:\n'

    str2 = ''

    str3 = 'clean:\n'

    for f in fList:

        str1 = str1 + '\tmake ' + f + '\n'

        str2 = ('%s%s:%s.o\n') % (str2,f,f)

        str2 = ('%s\t$(CC) -o %s %s.o\n\n') % (str2,f,f)

        str3 = ('%s\trm -f %s\n') % (str3,f)

    str3 = str3 + '\trm -f *.o\n'

    strClean = '.c.o:\n\t$(CC) $(CFLAGS) -c -o $*.o $<\n'

    msg = ('%s%s\n%s\n%s\n%s') % (msg,str1,str2,str3,strClean) 

    #print 'msg : \n'

    #print msg

    return msg
if __name__ == '__main__':

    str = genMakefileStr('.','.c')

    file = open("Makefile","w")

    file.write(str)

    file.close()

    print str

运行效果如下(示例):

# ./genMakefile.py          

CC = gcc

CFLAGS = -g -O2 -Wall
all:

        make pfun1

        make pfun2
pfun1:pfun1.o

        $(CC) -o pfun1 pfun1.o
pfun2:pfun2.o

        $(CC) -o pfun2 pfun2.o


clean:

        rm -f pfun1

        rm -f pfun2

        rm -f *.o
.c.o:

        $(CC) $(CFLAGS) -c -o $*.o $<

运行脚本后进行make即可。

附:

感觉上面的那个脚本用着不方便,随后修改修改,代码如下:

#! /usr/bin/python

'''

  File      : genMakefile.py

  Author    : Mike

  E-Mail    : Mike_Zhang@live.com

'''

import os,sys

 

surfix = ['.c','.cpp']
def genMakefileStr(dir):

    msg = ''

    msg = msg + 'CC = g++ ' + '\n'

    msg = msg +  'CFLAGS = -g -O2 -Wall' + '\n\n'

    

    fList = []

    for dirPath,dirNames,fileNames in os.walk(dir):

        for file in fileNames:

            name,extension = os.path.splitext(file)

            if surfix.count(extension) > 0:

                fList.append(name)

        break # only search the current directory

    str1 = 'all:\n'

    str2 = ''

    str3 = 'clean:\n'

    for f in fList:

        str1 = str1 + '\tmake ' + f + '\n'

        str2 = ('%s%s:%s.o\n') % (str2,f,f)

        str2 = ('%s\t$(CC) -o %s %s.o\n\n') % (str2,f,f)

        str3 = ('%s\trm -f %s\n') % (str3,f)

    str3 = str3 + '\trm -f *.o\n'

    strClean = '.c.cpp.o:\n\t$(CC) $(CFLAGS) -c -o $*.o $<\n'

    msg = ('%s%s\n%s\n%s\n%s') % (msg,str1,str2,str3,strClean) 

    #print 'msg : \n'

    #print msg

    return msg

 

if __name__ == '__main__':

    for arg in sys.argv[1:]:

        print arg

    str = genMakefileStr(arg)

    if arg[-1] == '/':arg = arg[:-1]

    file = open(arg+"/Makefile","w")

    file.write(str)

    file.close()

    print str

把文件genMakefile.py改名为genMakefile,复制到/usr/local/bin下,以后在需要的目录里面执行如下命令即可:

genMakefile .

Python 相关文章推荐
Python编程判断一个正整数是否为素数的方法
Apr 14 Python
Python3 适合初学者学习的银行账户登录系统实例
Aug 08 Python
python3 pillow生成简单验证码图片的示例
Sep 19 Python
Python冲顶大会 快来答题!
Jan 17 Python
Python3中详解fabfile的编写
Jun 24 Python
对Python 获取类的成员变量及临时变量的方法详解
Jan 22 Python
对Python强大的可变参数传递机制详解
Jun 13 Python
Python 解决OPEN读文件报错 ,路径以及r的问题
Dec 19 Python
python3的UnicodeDecodeError解决方法
Dec 20 Python
春节到了 教你使用python来抢票回家
Jan 06 Python
使用pytorch和torchtext进行文本分类的实例
Jan 08 Python
python dumps和loads区别详解
Feb 04 Python
Python和GO语言实现的消息摘要算法示例
Mar 10 #Python
Windows和Linux下使用Python访问SqlServer的方法介绍
Mar 10 #Python
Python脚本实现代码行数统计代码分享
Mar 10 #Python
Windows系统配置python脚本开机启动的3种方法分享
Mar 10 #Python
Python自动化构建工具scons使用入门笔记
Mar 10 #Python
Python操作CouchDB数据库简单示例
Mar 10 #Python
Python性能优化技巧
Mar 09 #Python
You might like
正义联盟的终局之战《天启星战争》将成为DC动画宇宙的最后一部
2020/04/09 欧美动漫
浅谈PHP的反射API
2017/02/26 PHP
基于PHP的微信公众号的开发流程详解
2020/08/07 PHP
Windows Live的@live.com域名注册漏洞 利用代码
2006/12/27 Javascript
不用ajax实现点击文字即可编辑的方法
2007/12/16 Javascript
ScrollDown的基本操作示例
2013/06/09 Javascript
javascript实现列表滚动的方法
2015/07/30 Javascript
JavaScript的面向对象编程基础
2015/08/13 Javascript
IE8兼容Jquery.validate.js的问题
2016/12/01 Javascript
浅析JavaScript的几种Math函数,random(),ceil(),round(),floor()
2016/12/22 Javascript
jQuery中animate()的使用方法及解决$(”body“).animate({“scrollTop”:top})不被Firefox支持的问题
2017/04/04 jQuery
妙用Angularjs实现表格按指定列排序
2017/06/23 Javascript
vue结合Echarts实现点击高亮效果的示例
2018/03/17 Javascript
微信小程序实现循环动画效果
2018/07/16 Javascript
python实现迭代法求方程组的根过程解析
2019/11/25 Javascript
微信小程序实现自定义底部导航
2020/11/18 Javascript
[54:05]DOTA2-DPC中国联赛定级赛 SAG vs iG BO3第一场 1月9日
2021/03/11 DOTA
python实现稀疏矩阵示例代码
2017/06/09 Python
python正则表达式面试题解答
2020/04/28 Python
Python的互斥锁与信号量详解
2019/09/12 Python
利用python在excel中画图的实现方法
2020/03/17 Python
Django admin 实现search_fields精确查询实例
2020/03/30 Python
python字典通过值反查键的实现(简洁写法)
2020/09/30 Python
浅谈html5之sse服务器发送事件EventSource介绍
2017/08/28 HTML / CSS
施华洛世奇美国官网:SWAROVSKI美国
2018/02/08 全球购物
铭万公司.net面试题笔试题
2014/07/20 面试题
小学生演讲稿
2014/01/12 职场文书
将相和教学反思
2014/02/04 职场文书
《大江保卫战》教学反思
2014/04/11 职场文书
优秀毕业生就业推荐信
2014/05/22 职场文书
学校消防安全责任书
2014/07/23 职场文书
四查四看自我剖析材料
2014/09/19 职场文书
导游欢送词
2015/01/31 职场文书
2015年工商所工作总结
2015/05/21 职场文书
百家讲坛观后感
2015/06/12 职场文书
python3使用diagrams绘制架构图的步骤
2021/04/08 Python