版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
Windows Python实现C++多文件编译脚本(没有Make怎么办)
因为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)