在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 .
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
