Under Linux to write a few test programs, but also a line of input g++ command to compile, when often changed to test code, that time and again (or the line arrow election) also feel uncomfortable, not as fast as make. With the benefit of makefile not to say more, here I wrote a script, its function is to automatically search the current directory (excluding subdirectories) under the ". C" file to generate makefile files.
The code here, the function is limited (for a single file is a separate test code case), the need for friends can be slightly modified to meet the needs.
Copy the Code code as follows:
#! /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 '
flist = []
for Dirpath,dirnames,filenames in Os.walk (dir):
for file in FileNames:
Nam e,extension = os.path.splitext (file)
If extension = = Surfix:
flist.append (name)
Break # only search the Curren T 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
The performance is as follows (example):
Copy the Code code as follows:
#./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 it after running the script.
Report:
I feel that the script above is inconvenient, then modified, the code is as follows:
Copy the Code code as follows:
#! /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 '
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 # Search for 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
Rename the file genmakefile.py to Genmakefile, copy it to/usr/local/bin, and then execute the following command in the desired directory:
Genmakefile.