Write several tests in LinuxProgram, You need to enter the G ++ command in one line for compilation. when the test is often changedCodeIn this case, it is not as easy as making. The advantage of using makefile is unnecessary. Here I wrote a script to automatically search for the. c file under the current directory (excluding subdirectories) to generate the makefile.
The code here has limited functions (applicable when a single file is an independent test code). You can make some modifications to your needs.
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 '
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. O \ 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 ' ) % (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
The running effect is as follows (example ):
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 $ <
Run the script and run make.
Appendix:
The above script is inconvenient to use, and then the modification code is 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 # Only search the current directory Str1 = ' ALL: \ n ' Str2 = '' Str3 = ' Clean: \ n ' For FIn Flist: str1 = Str1 + ' \ Tmake ' + F + ' \ N ' Str2 = ( ' % S % s: % S. O \ n ' ) % (Str2, F, F) str2 = ( ' % S \ t $ (CC)-O % S. O \ 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 ' ) % (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 genmakefile. py to genmakefile and copy it to/usr/local/bin. Then, execute the following command in the required directory:
Genmakefile.