Python实现生成简单的Makefile文件代码示例

yipeiwu_com5年前Python基础

在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 .

相关文章

pytorch 在网络中添加可训练参数,修改预训练权重文件的方法

实践中,针对不同的任务需求,我们经常会在现成的网络结构上做一定的修改来实现特定的目的。 假如我们现在有一个简单的两层感知机网络: # -*- coding: utf-8 -*- im...

详解MySQL数据类型int(M)中M的含义

介绍 MySQL 数据类型中的 integer types 有点奇怪。你可能会见到诸如:int(3)、int(4)、int(8) 之类的 int 数据类型。刚接触 MySQL 的时候,我...

Python pandas常用函数详解

本文研究的主要是pandas常用函数,具体介绍如下。 1 import语句 import pandas as pd import numpy as np import matplot...

对python 命令的-u参数详解

缘起: 今天在看arcface的训练代码,在shell脚本中运行python 命令时后面加了-u 参数(python -u xx.py),于是对这个参数进行了下小研究。 准备知识 用网上...

对python以16进制打印字节数组的方法详解

对python以16进制打印字节数组的方法详解

一、问题描述 如果直接用print打印bytes的话,有时候会直接显示ascii对应的字符,看起来很蛋疼。 二、运行效果 上面一行是直接用print打印的结果,很明显,第一个字节0x7...