繁体   English   中英

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

[英]Compiling C++ code with g++ from Python script on Windows

我正在尝试编写一个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而且这通常是一件小事,所以我现在对于我做错了什么很难过。

它不是找不到的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而不是与当前工作目录相关的其他地方,保持为true。

我能够解决自己的问题,并使用以下代码获得一个有效的.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有点笨拙,但似乎必须首先在命令中引用主文件。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM