您好,欢迎访问一九零五行业门户网

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

在linux下写几个测试程序,还要一行行的输入g++命令进行编译,当经常改测试代码的时候,那一次次的敲(或者一次次的上线箭头选)也感觉不爽,不如make来的快。用makefile的好处就不用多说了,这里我写了个脚本,其功能是自动搜索当前目录(不包括子目录)下的“.c”文件生成makefile文件。
代码在这里,功能有限(适用于单个文件是一个独立的测试代码的情况),需要的朋友可以稍作修改以满足需求。
复制代码 代码如下:
#! /usr/bin/python
'''
 file      : genmakefile.py
 author    : mike
 e-mail    : mike_zhang@live.com
'''
import osdef 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 $    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 -wallall:
        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 $    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 .
其它类似信息

推荐信息