我正在编写一个Python脚本,该脚本通过subprocess.Popen()调用subprocess.Popen,并使用它将.cpp文件编译成.exe。问题是,无论我如何尝试将路径传递给源文件,我都会得到以下错误:
g++.exe:错误: CreateProcess:没有这样的文件或目录
我的目录结构如下:
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很多次,这通常是一件琐碎的事情,所以我现在对自己做错了什么感到困惑。
发布于 2016-06-12 14:04:35
找不到的不是client.cpp文件,而是g++.exe。您可以知道这一点,因为生成错误的是CreateProcess。如果是cpp文件,CreateProcess就会成功,只有这样编译器才会返回错误。
os.path.abspath("external/mingw64/g++.exe")这将从您提供的相对路径构建一个绝对路径。相对意思是相对于当前目录,而不是相对于python文件的目录。
如果您的g++位于固定的树中,更好的方法应该是从脚本名构造路径,如下所示:
os.path.join(os.path.dirname(__file__), "external/mingw64/g++.exe")对于使用abspath进行与当前工作目录无关的事情的其他地方,也是如此。
发布于 2016-06-13 19:01:07
我能够解决自己的问题,并使用以下代码获得一个工作的.exe:
import os, subprocess, json, glob
class client():
def __init__(self):
self.gccDir = os.path.abspath("external/mingw64")
self.sourceDir = "c/client"
self.fileName = "client.cpp"
self.sourceFile = os.path.join(self.sourceDir, self.fileName)
self.destFile = self.sourceFile.replace(".cpp", ".exe")
def run(self):
srcFiles = glob.glob(os.path.join(self.sourceDir+"/*.cpp"))
srcFiles.remove(self.sourceFile)
myEnv = os.environ.copy()
myEnv["PATH"] = myEnv["PATH"]+";"+self.gccDir
command = ["g++.exe", self.sourceFile, " ".join([x for x in srcFiles]), "-std=c++11", "-Os", "-o", self.destFile]
process = subprocess.Popen(command, shell=True, env=myEnv, 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该命令最后是:
['g++.exe', 'c/client\\client.cpp', 'c/client\\utils.cpp', '-std=c++11', '-Os', '-o', 'c/\\client.exe']路看上去很难看,但很有用。手动从sourceFile中删除srcFiles有点笨拙,但似乎有必要在命令中首先引用主文件。
This answer非常有用,它允许我将PATH环境变量临时设置为g++.exe所在的任何目录。感谢大家的帮助。
https://stackoverflow.com/questions/37774886
复制相似问题