大佬教程收集整理的这篇文章主要介绍了Python实现生成简单的Makefile文件代码示例,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
在linux下写几个测试程序,还要一行行的输入g++命令进行编译,当经常改测试代码的时候,那一次次的敲(或者一次次的上线箭头选)也感觉不爽,不如make来的快。用Makefile的好处就不用多说了,这里我写了个脚本,其功能是自动搜索当前目录(不包括子目录)下的“.c”文件@L_710_6@makefile文件。
代码在这里,功能有限(适用于单个文件是一个独立的测试代码的情况),需要的朋友可以稍作修改以满足需求。
def genMakefileStr(dir,surfix = '.c'):@H_262_11@ msg = ''@H_262_11@ msg = msg + 'CC = gcc' + '\n'@H_262_11@ msg = msg + 'CFLAGS = -g -O2 -Wall' + '\n\n'@H_262_11@ @H_262_11@ fList = []@H_262_11@ for dirPath,dirNames,filenames in os.walk(dir):@H_262_11@ for file in filenames:@H_262_11@ name,extension = os.path.splitext(filE)@H_262_11@ if extension == surfix:@H_262_11@ fList.append(Name)@H_262_11@ break # only search the current directory@H_262_11@ str1 = 'all:\n'@H_262_11@ str2 = ''@H_262_11@ str3 = 'clean:\n'@H_262_11@ for f in fList:@H_262_11@ str1 = str1 + '\tmake ' + f + '\n'@H_262_11@ str2 = ('%s%s:%s.o\n') % (str2,f,f)@H_262_11@ str2 = ('%s\t$(CC) -o %s %s.o\n\n') % (str2,f)@H_262_11@ str3 = ('%s\trm -f %s\n') % (str3,f)@H_262_11@ str3 = str3 + '\trm -f *.o\n'@H_262_11@ strClean = '.c.o:\n\t$(CC) $(CFLAGS) -c -o $*.o $<\n'@H_262_11@ msg = ('%s%s\n%s\n%s\n%s') % (msg,str1,str2,str3,strClean) @H_262_11@ #print 'msg : \n'@H_262_11@ #print msg@H_262_11@ return msg
if __name__ == '__main__':@H_262_11@ str = genMakefileStr('.','.c')@H_262_11@ file = open("Makefile","w")@H_262_11@ file.write(str)@H_262_11@ file.close()@H_262_11@ print str@H_262_11@ @H_489_9@
运行效果如下(示例):
all:@H_262_11@ make pfun1@H_262_11@ make pfun2
pfun1:pfun1.o@H_262_11@ $(CC) -o pfun1 pfun1.o
pfun2:pfun2.o@H_262_11@ $(CC) -o pfun2 pfun2.o
@H_262_11@ clean:@H_262_11@ rm -f pfun1@H_262_11@ rm -f pfun2@H_262_11@ rm -f *.o
.c.o:@H_262_11@ $(CC) $(CFLAGS) -c -o $*.o $<@H_262_11@ @H_489_9@
运行脚本后进行make即可。
附:
感觉上面的那个脚本用着不方便,随后修改修改,代码如下:@H_262_11@
def genMakefileStr(dir):@H_262_11@ msg = ''@H_262_11@ msg = msg + 'CC = g++ ' + '\n'@H_262_11@ msg = msg + 'CFLAGS = -g -O2 -Wall' + '\n\n'@H_262_11@ @H_262_11@ fList = []@H_262_11@ for dirPath,extension = os.path.splitext(filE)@H_262_11@ if surfix.count(extension) > 0:@H_262_11@ fList.append(Name)@H_262_11@ break # only search the current directory@H_262_11@ str1 = 'all:\n'@H_262_11@ str2 = ''@H_262_11@ str3 = 'clean:\n'@H_262_11@ for f in fList:@H_262_11@ str1 = str1 + '\tmake ' + f + '\n'@H_262_11@ str2 = ('%s%s:%s.o\n') % (str2,f)@H_262_11@ str3 = str3 + '\trm -f *.o\n'@H_262_11@ strClean = '.c.cpp.o:\n\t$(CC) $(CFLAGS) -c -o $*.o $<\n'@H_262_11@ msg = ('%s%s\n%s\n%s\n%s') % (msg,strClean) @H_262_11@ #print 'msg : \n'@H_262_11@ #print msg@H_262_11@ return msg@H_262_11@ @H_262_11@ if __name__ == '__main__':@H_262_11@ for arg in sys.argv[1:]:@H_262_11@ print arg@H_262_11@ str = genMakefileStr(arg)@H_262_11@ if arg[-1] == '/':arg = arg[:-1]@H_262_11@ file = open(arg+"/Makefile","w")@H_262_11@ file.write(str)@H_262_11@ file.close()@H_262_11@ print str@H_262_11@ @H_489_9@
把文件genMakefile.py改名为genMakefile,复制到/usr/local/bin下,以后在需要的目录里面执行如下命令即可:
genMakefile .
以上是大佬教程为你收集整理的Python实现生成简单的Makefile文件代码示例全部内容,希望文章能够帮你解决Python实现生成简单的Makefile文件代码示例所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。