当前位置: 移动技术网 > IT编程>脚本编程>Python > Windows下Python3在没有Make的情况下实现C++多文件编译脚本

Windows下Python3在没有Make的情况下实现C++多文件编译脚本

2018年03月12日  | 移动技术网IT编程  | 我要评论

别找巨星当媳妇儿,纸业综合信息网,咱们工人有力量 歌词

Windows下Python3在没有Make的情况下实现C++多文件编译脚本

因为Windows上没有make,所有写了个编译脚本。

Python3 写的脚本。

相比Make,不能动态监测更改局部更新编译,可以试着加上diff。

所以只适合少量多文件编译

功能有:

1. 删除之前编译的中间文件

2. 自动编译生成exe文件

3. 自动运行exe文件

代码

代码块语法遵循标准markdown代码,例如:

#coding=utf-8
import os,sys
def delfile(file):
    if(os.path.exists(file)):
        print(file)
        os.remove(file)

#参数配置
    #需要编译的文件 更改这里 也可以通过读目录下的所有文件自动录入只有cpp和h的文件
files=['Rational.h','Rational.cpp','test.cpp']
    #输出exe文件名
output="Test"

    #是否删除之前编译的文件,如果有的话. 0 否 1 是
isdel=1
    #是否编译 0 否 1 是
ismake=1
    #是否自动运行 0 否 1 是
isrun=0

#一键清除文件重新编译
if(isdel>0):
    files_name=[]
    print("[*] 开始删除文件")
    for i in files:
        if(i.find(".")>=0):
            files_name.append(i[0:i.find(".")])
    for root,dirs,allfiles in os.walk("."):
        for i in allfiles:
            for j in files_name:
                if(i.find(j)>=0):
                    if(i.find(".cpp")==-1 and i.find(".h")==-1):
                        delfile(i)
                    if(i.find(".h.gch")>=0):
                        delfile(i)
    if(os.path.exists(output+".exe")):
        print(output+".exe")
        os.remove(output+".exe")
    print("[*] 删除完毕")


#一键编译所有文件

if(ismake>0):
    cmd1="g++ -c"
    for i in files:
        cmd1=cmd1+" "+i
    cmd2="g++ -o "+output
    for i in files:
        ind=i.find('.cpp')
        if(ind>=0):
            cmd2 = cmd2 + ' '+i[0:ind]+".o"
    print("[*] "+cmd1)
    print("------Running-------")
    os.system(cmd1)
    print("[*] "+cmd2)
    os.system(cmd2)
    print("------Running-------")


#自动运行编译好的文件
if(isrun>0):
    cmd3=output+".exe"
    os.system(cmd3)

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网