我试图编写一个Python脚本,它通过subprocess.Popen()调用g ++。exe文件并使用它将.cpp文件编译为一个.exe文件。问题是,不管我怎么努力的路径传递到源文件,我得到以下错误:在Windows上使用g ++从Python脚本编译C++代码
g++.exe: error: CreateProcess: No such file or directory
我的目录结构如下:
D:/Test/test.py
D:/Test/external/mingw64/g++.exe
D:/Test/c/client/client.cpp
而且我的代码是:
import os, subprocess
class builder():
def __init__(self):
self.gccPath = os.path.abspath("external/mingw64/g++.exe")
self.sourceDir = os.path.abspath("c/client")
self.fileName = "client.cpp"
self.sourceFile = os.path.join(self.sourceDir, self.fileName)
def run(self):
command = [self.gccPath, self.sourceFile , "-o", "client.exe"]
print command
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
n=1
while True:
nextLine = process.stdout.readline()
if nextLine == '' and process.poll() != None:
break
if nextLine != "" and nextLine != None:
print n, nextLine
n=n+1
builder = builder()
builder.run()
只是一些我试图通过该路径的方式:
Command: ["D:\\Test\\external\\mingw64\\g++.exe", "c/client/client.cpp", "-o", "client.exe"]
Command: ["D:\\Test\\external\\mingw64\\g++.exe", "c\\client\\client.cpp", "-o", "client.exe"]
Command: ["D:\\Test\\external\\mingw64\\g++.exe", "D:\\Test\\c\\client\\client.cpp", "-o", "client.exe"]
我也试过路过CWD到POPEN:
command = [self.gccPath, "client.cpp", "-o", "client.exe"]
process = subprocess.Popen(command, shell=True, cwd=self.sourceDir, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
总是同样的错误。我之前使用过Popen很多次,这通常都是一件小事,所以我现在很害怕我做错了什么。
@ PM2Ring - 但我没有加入两条绝对路径,我加入了一个字符串的绝对路径。 我并不太在意路径外观,它们只是为了调试目的而被打印出来。如果当我得到这个工作,所有打印将被删除。 – Natsukane
听起来像你正在尝试构建一个构建系统。为什么不使用现有的像SCons(http://scons.org/)或CMake(https://cmake.org/)? –