在Windows上使用Python脚本中的g ++编译C ++代码

时间:2016-06-12 13:35:49

标签: python c++ g++ popen

我正在尝试编写一个Python脚本,通过subprocess.Popen()调用g ++。exe并使用它将.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而且这通常都是一件小事,所以我现在很难知道我做错了什么。

2 个答案:

答案 0 :(得分:0)

不是找不到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的其他地方,对于与当前工作目录无关的内容,则保持为真。

答案 1 :(得分:0)

我能够解决自己的问题并使用以下代码获得正常工作的.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']

路径看起来很难看但很有效。手动从srcFiles中删除sourceFile有点笨拙,但似乎必须首先在命令中引用主文件。

This answer非常有用,并允许我暂时将PATH环境变量设置为我有g ++。exe的目录。感谢大家试图提供帮助。