Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
Windows Python implement C ++ multi-file compilation script (not Make how to do)
Because there is no make on Windows, all wrote a script compiler.
Python3 write the script.
Compared Make, can not change the dynamic monitoring of the local compiler updates, you can try to add diff.
So only suitable for a small amount of multi-file compilation
features are:
1. compiler intermediate files before deleting
2. automatically compiled exe file
3. autorun exe file
Code
Markdown syntax follows the standard block code, for example:
#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)