日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

生成简单的Makefile文件(Python实现)

發布時間:2023/12/20 python 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 生成简单的Makefile文件(Python实现) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在linux下寫幾個測試程序,還要一行行的輸入g++命令進行編譯,當經常改測試代碼的時候,那一次次的敲(或者一次次的上線箭頭選)也感覺不爽,不如make來的快。用Makefile的好處就不用多說了,這里我寫了個腳本,其功能是自動搜索當前目錄(不包括子目錄)下的“.c”文件生成Makefile文件。
代碼在這里,功能有限(適用于單個文件是一個獨立的測試代碼的情況),需要的朋友可以稍作修改以滿足需求。

1 #! /usr/bin/python
2 '''
3 File : genMakefile.py
4 Author : Mike
5 E-Mail : Mike_Zhang@live.com
6 '''
7 import os
8
9 def genMakefileStr(dir,surfix = '.c'):
10 msg = ''
11 msg = msg + 'CC = gcc' + '\n'
12 msg = msg + 'CFLAGS = -g -O2 -Wall' + '\n\n'
13
14 fList = []
15 for dirPath,dirNames,fileNames in os.walk(dir):
16 for file in fileNames:
17 name,extension = os.path.splitext(file)
18 if extension == surfix:
19 fList.append(name)
20 break # only search the current directory
21 str1 = 'all:\n'
22 str2 = ''
23 str3 = 'clean:\n'
24 for f in fList:
25 str1 = str1 + '\tmake ' + f + '\n'
26 str2 = ('%s%s:%s.o\n') % (str2,f,f)
27 str2 = ('%s\t$(CC) -o %s %s.o\n\n') % (str2,f,f)
28 str3 = ('%s\trm -f %s\n') % (str3,f)
29 str3 = str3 + '\trm -f *.o\n'
30 strClean = '.c.o:\n\t$(CC) $(CFLAGS) -c -o $*.o $<\n'
31 msg = ('%s%s\n%s\n%s\n%s') % (msg,str1,str2,str3,strClean)
32 #print 'msg : \n'
33 #print msg
34 return msg
35
36 if __name__ == '__main__':
37 str = genMakefileStr('.','.c')
38 file = open("Makefile","w")
39 file.write(str)
40 file.close()
41 print str

運行效果如下(示例):

1 # ./genMakefile.py
2 CC = gcc
3 CFLAGS = -g -O2 -Wall
4
5 all:
6 make pfun1
7 make pfun2
8
9 pfun1:pfun1.o
10 $(CC) -o pfun1 pfun1.o
11
12 pfun2:pfun2.o
13 $(CC) -o pfun2 pfun2.o
14
15
16 clean:
17 rm -f pfun1
18 rm -f pfun2
19 rm -f *.o
20
21 .c.o:
22 $(CC) $(CFLAGS) -c -o $*.o $<

運行腳本后進行make即可。

附:

感覺上面的那個腳本用著不方便,隨后修改修改,代碼如下:

#! /usr/bin/python '''File : genMakefile.pyAuthor : MikeE-Mail : Mike_Zhang@live.com ''' import os,syssurfix = ['.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 directorystr1 = '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 msgreturn msgif __name__ == '__main__':for arg in sys.argv[1:]:print argstr = 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 .

本文github地址:

https://github.com/mike-zhang/mikeBlogEssays/blob/master/2011/生成簡單的Makefile文件(Python實現).txt

歡迎補充?

轉載于:https://www.cnblogs.com/MikeZhang/archive/2012/01/17/genMakefileTest1.html

總結

以上是生活随笔為你收集整理的生成简单的Makefile文件(Python实现)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。