在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下,之后在必须的文件目录里边实行以下指令就可以:文章内容来源于:www.seo-7.comwww.sEo-6.comhttp://www.seo-6.com/seoyh/seojichurm/117591.html
(编辑:部分内容来互联网)